Connecting to Office 365 with PowerShell can be very useful to an admin of an Office 365 environment.
Assigning a few licenses can be done easily through the admin portal (https://portal.microsoftonline.com), however when assigning licenses in bulk an admin will quickly find himself in need of a more efficient way.
This is where PowerShell can assist. Scripting time consuming tasks will improve the efficiency of the admin enormously. In some cases the time was reduced by 80%!
However before PowerShell can be used to connect to Office 365, the prerequisites must be met.
The first thing you need to run the scripts from a machine with either Windows 7 or Windows Server 2008 R2 installed (or higher). You need to have PowerShell 3.0 installed as well as .NET framework 3.5.
PowerShell 3.0 is part of Windows Management Framework 3.0:
Windows Management Framework 3.0
Next you will need to download and installthe Microsoft Online Sign In Assistant:
Microsoft Online Services Sign-In Assistant
Then you will need to download and install the Windows Azure AD Module for Windows PowerShell:
Windows Azure AD Module for Windows PowerShell 64-bit
Windows Azure AD Module for Windows PowerShell32-bit
Now it’s time to start PowerShell.
Hit the Windows key and type powershell (it will show up in the search results at the top).
First you will need to import the MSOnline module, which is nothing more than:
After this you roughly have two options, the choice between the two depends on the security situation. You can choose to get a credentials popup or the include login information in plain text in the script. Safest is the use the credentials popup; only use the hard coded password when you are absolutely sure that no unauthorized person can access the location where the scripts are situated (for example on a server where only the Office 365 admins have access).
When using the popup for logging in use the following line:
$creds = Get-Credentials
When using the plain text storage of password and account name use the following lines. Replace [upn] with you full UPN (username@domain) and replace [password] with your password:
$User = "[upn]" $Pass = "[password]" $creds = New-Object System.Management.Automation.PSCredential($User,(ConvertTo-SecureString $Pass -AsPlainText -Force));
When the credentials are stored safely in the $creds variable, use it to connect to the Microsoft Online Service:
Connect-MsolService -Credential $creds
If you also want to connect to SharePoint Online service also run the next line (replace [tenant] with you own tenant name):
Connect-SPOService -Url https://[tenant]-admin.sharepoint.com -Credential $creds
Now you are connected to Office 365 (and perhaps also to SharePoint Online) and are ready to execute some cool cmdlets the make your work easier and more efficient.
In a later post I will dig deeper in some handy cmdlets for Microsoft Online and SharePoint online.
Below I have an example script you may use as a logon script, make sure you run the script before executing other Office 365 scripts and you don’t need to worry about memorizing all previous commands (be sure to replace the placeholders with your values, and to choose the right credential method for your situation):
Write-Host "Importing Module MSOnline" -ForegroundColor Green Import-Module MSOnline $User = "[username]@[tenant].onmicrosoft.com" $Pass = "[password]" $creds = New-Object System.Management.Automation.PSCredential($User,(ConvertTo-SecureString $Pass -AsPlainText -Force)); #$creds = Get-Credentials; Write-Host "Connecting To Microsoft Online Service" -ForegroundColor Green Connect-MsolService -Credential $creds Write-Host "Connecting To SharePoint Online Service" -ForegroundColor Green Connect-SPOService -Url https://[tenant]-admin.sharepoint.com -Credential $creds