PowerShell – Setting User name Get-Credentials to log into Office 365

The PowerShell command Get-Credential is used to collect user credentials.

login1

When I try to connect to Office 365 I’m suing this dialog to collect the login details of a user. I find it however very annoying that I have to keep supplying the user’s name and password. Ok, password is fine but the username is annoying especially with the long Office 365 logins ( ….@mytenant.onmicrosoft.com).

When I looked at the options for Get-Credential I noticed -Username. This is a great option as I can now do something like:

Get-Credential -UserName "pieter.veenstra@mytenant.onmicrosoft.com"

But no, still an empty login window.

login1

Then I tried the -Message option. Why would this possibly be relevant???

Get-Credential -Message "Supply password" -UserName "pieter.veenstra@mytenant.onmicrosoft.com"

login

The username is now set and I can type in my password. Well that saves be some time during the development of my PowerShell scripts.

Now a little check around the code and I only need to sign in once:

if ( $cred -ne $null)
{
  $cred = Get-Credential -Message "Supply password" -UserName "pieter.veenstra@mytenant.onmicrosoft.com"
  Connect-SPOnline -Url "https:/ /mytenant.sharepoint.com" -Credentials $cred
}
Advertisements


Categories:Office 365, PowerShell, SharePoint, SharePoint Online

Tags: ,

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.