connect-msolservice -credentials not working

Hi ALl
I use this code to store my credentials through out the rest of my script this is working just fine.

 $username = $env:USERNAME
write-host "check if Credential file is available please wait......"
if(!(Test-Path "c:\temp\cred\$username.Cred")){
    $Credential = Get-Credential
    $Credential | Export-CliXml -Path "c:\temp\cred\$username.Cred"
    } Else {
        write-host "credentials found"

however when I use connect-msolservice -credentials $credential I still get the popup to enter my username and password
when I use Connect-AzureAD -credentials $credential I don’t get the popup window to enter my username and password

PS C:\temp\powershell> Connect-AzureAD -Credential $credential

Account                    Environment TenantId                             TenantDomain  AccountType
-------                    ----------- --------                             ------------  -----------       AzureCloud  mytenantid                  User

what do I need to do to make sure that the username and password are correctly handled when I connect to msolservice so that I don’t get the popup screen
thanks for your assistance


The expected behavior of Get-Credential is to prompt for username and password (every time). You can use the -Username parameter to pre-fill the username, but you should always be prompted to input your password. It’s possible to create a credential object without prompting the user (as in Example 6) but it requires the password to be stored in a variable as plain text, so probably not a great idea.

When exactly are you getting the popup? When you run your script, or when you run Connect-MsolService?

This discussion seems relevant: powershell - How to use connect-msolservice with automated credentials? - Stack Overflow
And apparently the MSOnline module is being deprecated.

Also, based on the documentation for Connect-MsolService and Connect-AzureAD they have different versions of PowerShell implemented. There may be a difference in the way they’re handled on the server side, and if the MSOnline module is being phased out it may be a problem that never gets fixed.