Manage Office 365 with PowerShell

The core components of Office 365 are Exchange Online, Lync Online and SharePoint Online, all are running on top of Windows Azure Active Directory as shown in the following figure:


All services can be managed from the Microsoft Online Portal. When logged on to the portal you can select the various services under Admin in the navigation pane. It is also possible to manage Office 365 using PowerShell, but all services require a different approach or module.

Managing Windows Azure Active Directory using PowerShell

To manage Windows Azure Active Directory with PowerShell you have to install the Azure Active Directory Module for Windows PowerShell (64-bit version) but before you can use this you also have to install the Microsoft Online Services Sign-In Assistant.

When you have these installed you can open PowerShell on your machine and connect to the Azure Active Directory using the following commands:

$Cred = Get-Credentials
Connect-MsolService $Cred


With this PowerShell module are command related to Microsoft Online (Msol) are available. To get an overview of these commands, you can use the Get-Command command. Filter on the name of the commands (*msol*) and output to file, like this:

Get-Command | Where {$_.Name -like "*msol*"} | Out-File C:\Temp\MsolCommands.txt

When you use notepad to open this file you can examine the results:


You can use the Get-MsolDomain command to retrieve a list of all domains available in your Office 365. In our test environment it’s only one additional domain, the domain we added in the previous blogpost as shown in the following figure:


Similar to this you can use the Get-MsolUser command to retrieve all users in Windows Azure Active Directory. Running the Get-Msoluser command in our test environment shows all users that were created in the previous blog post:


More information on Managing Azure Active Directory using PowerShell can be found on the following MSDN page:

Managing Exchange Online using PowerShell

When you want to manage Exchange Online using PowerShell there’s no need to install a separate module for PowerShell. You only have to setup a Remote PowerShell session to Exchange Online and all commands are automatically transferred to the local PowerShell.

To setup a Remote PowerShell session you can use the following commands in a local PowerShell window:

$ExCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $ExCred -Authentication Basic -AllowRedirection
Import-PSSession $Session

When the session is setup all Exchange commands are available. For example, when running the Get-Mailbox commands all Mailboxes that are created in Exchange Online are available are shown on the console. In my test environment you can see that the Mailboxes are spread across Amsterdam (ServerName starts with ‘am’) and Dublin (ServerName starts with ‘db’) as shown in the following figure:


To close the Remote PowerShell session you can use the Remove-PSSession $Session command.

More information regarding Exchange Online PowerShell can be found on TechNet:

Managing Lync Online using PowerShell

The first step in managing Lync Online using PowerShell is installing the Lync Online Connector module which can be downloaded from Once installed you can use the following commands to import the Lync Online Connector and setup a session with Lync Online:

Import-Module LyncOnlineConnector
$LyncCred = Get-Credential
$Session = New-CsOnlineSession -Credential $LyncCred
Import-PSSession $Session


All command regarding Lync Online are now available. For example, to retrieve all Lync Enabled accounts in the test environment we can use the Get-CsOnlineUser command as shown in the following figure:


More information regarding Using Windows PowerShell to manage Lync Online can be found on TechNet:

Managing SharePoint Online using PowerShell

Right now we’re getting used to it, but for managing SharePoint Online using PowerShell the SharePoint Online Management Shell needs to be installed. You can download this from the Microsoft Download site: This will install a (dedicated) SharePoint Online Management Shell.

To setup a session to SharePoint Online you can use the following commands:

$SPCred = Get-Credential
Connect-SPOService -Url -Credential $SPCred


Clearly visible is the SharePoint Online Management Shell compared to a regular PowerShell command prompt. Also note the different backgroud color.

To retrieve a list of Site Collections available in your SharePoint Online environment you can use the Get-SPOSite command as shown in the following figure:


More information about Windows PowerShell for SharePoint Online can be found on TechNet:


Besides the regular Microsoft Online Portal you can also use PowerShell to manage to core components of your Office 365 environment. Every service has a slightly different approach, but when installing the proper PowerShell modules you can everything from a local PowerShell command prompt.

In the following blog post I’ll go more into detail about the various possibilities.

4 thoughts on “Manage Office 365 with PowerShell”

Leave a Reply to jaapwesselius Cancel reply

Please log in using one of these methods to post your comment: Logo

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

Facebook photo

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

Connecting to %s