Office 365 web interface was designed to make it easier to use right down to its administrative bowels. On the one hand it really is quick and simple to navigate, on the other it definitely lacks some advanced configuration options so loved by sysadmins.
Luckily there is the mighty PowerShell coming to the rescue! You should already know its potential, which can also be utilized in Office 365. Below you’ll find out how.
To remotely manage Office 365 with PowerShell your machine must meet the following requirements:
- Operating system: Windows 7, Windows 8, Windows Server 2008 R2, or Windows Server 2012.
- Microsoft .NET Framework 3.51
- Microsoft Online Services Sign-in Assistant
- Office 365 PowerShell cmdlets:
Once you have all the components installed you are ready to connect to Office 365.
Connecting to Office 365 with PowerShell
First you need to import all the relevant cmdlets. To do so in PowerShell command window run
Next run the code below and enter your logon credentials (your Office 365 administrative user email and password).
$365Logon = get-credential
Now configure your remote PowerShell session variables.
$365PSSession = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $365Logon -Authentication Basic -AllowRedirection
Run the code below to connect.
Import-PSSession $365PSSession -AllowClobber
If you encounter any runtime errors at this point, it is most probably because of the disabled script execution policy. You can enable it running the command below:
The final step is to connect to your Office 365 online service
Connect-MsolService –Credential $365Logon
That’s it! You are now ready to run PowerShell commands.
Below you can find the whole process in one script, which you can save to the PS1 file.
Import-Module MSOnline $365Logon = get-credential $365PSSession = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $365Logon -Authentication Basic -AllowRedirection Import-PSSession $365PSSession -AllowClobber Set-ExecutionPolicy RemoteSigned Connect-MsolService –Credential $365Logon
Whenever you need to remotely administer your Office 365 with PowerShell simply run the PS1 script with the code above. Alternatively, you can add it to your PowerShell profile so you are ready to rock automatically right after launching PS or PowerShell ISE.
To learn about available Exchange Online cmdlets see this TechNet article.