How To Connect-MsolService without Login Prompt

image 3 - How To Connect-MsolService without Login Prompt

To use PowerShell to manage Microsoft 365 services, you need to initiate a connection to Azure Active Directory first and then run the cmdlets it provides.


It prompts a normal Microsoft login screen for you to sign in. When you are using PowerShell window, one connection should last until you close the window. But if you are using PowerShell ISE or tools like Visual Studio Code to program a block of code to run it’s so inconvenient it pops up every time.

One way to make it easy is to use the PSCredential object. Use something like below at the beginning of your code, you should be able to bypass the login prompt during your code development.

$username = '[email protected]'
$pwd = ConvertTo-SecureString 'realpassword' -asplaintext -force;
$cred = New-Object -TypeName PSCredential -argumentlist $username, $pwd

Connect-MsolService -Credential $cred
##put more msonline codes below

You may consider removing it after you are done your coding though. Hard coded credentials in the code base is never a good practice.

Leave a Reply

Your email address will not be published. Required fields are marked *