Essential Guide

The essential admin's guide to Office 365 PowerShell

A comprehensive collection of articles, videos and more, hand-picked by our editors
Manage Learn to apply best practices and optimize your operations.

Start using PowerShell for Office 365, Exchange Online

PowerShell allows Office 365 and Exchange Online administrators to manage the service and complete tasks quickly and efficiently through the command line.

Exchange Online, Azure Active Directory and Office 365 all work together in many ways, and to efficiently administer...

them, you should learn to work with PowerShell.

If your company has Office 365, or just Exchange Online, you probably know how to administer each service via the Office 365 portal already. However, to make changes in bulk, you need the help of PowerShell scripting. Here's how to make a connection to Office 365 and Exchange Online with PowerShell.

PowerShell for Office 365

To connect to Office 365 with PowerShell requires a couple prerequisites that don't come installed on Windows by default: the Microsoft Online Services Sign-In Assistant and the Microsoft Azure Active Directory (AD) PowerShell Module. These are both Windows Installer MSI files that you must download and install, accepting the defaults. You only have to install these once.

Note: This article is based on use of version 0.98 of the Azure AD module.

Ensure that PowerShell can see the Azure Active Directory PowerShell module before starting. Open up a PowerShell console type Get-Module –Name MSOnline -ListAvailable (see figure 1).

Using PowerShell for Office 365
Figure 1. This screenshot shows the typical results in a PowerShell console for the command Get-Module –Name MSOnline -ListAvailable.

Once you confirm that the Azure AD module is installed correctly, run the Connect-MsolService cmdlet. This cmdlet attempts to create a connection with Azure AD. Office 365 depends on Azure AD for its user database and authentication.

When you run Connect-MsolService, you'll see a prompt to provide your credentials (see figure 2).

Credentials required for Azure AD.
Figure 2. Create a connection with Azure AD by entering your credentials.

You can also pass a PSCredential object to Connect-MsolService by assigning a variable to a credential object such as $MyCredential = Get-Credential and then passing that credential to Connect-MsolService by the –Credential parameter.

If all goes well, you'll be back in the PowerShell console with a connection made for Office 365. As a test, run Get-MsolDomain. This should return each of the domains you have registered with Office 365.

PowerShell for Exchange Online

Connecting to Exchange Online with PowerShell is easy -- it does not require any extra software to install. This connection simply relies on PowerShell's remote sessions to connect to the Exchange Online endpoint and to import all of the available cmdlets.

First, create a PSCredential object using Get-Credential:

$UserCredential = Get-Credential

Next, build a new PowerShell remoting session with the Exchange Online endpoint, passing it your username/password credential. This will establish a connection to Exchange Online for PowerShell:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $UserCredential -Authentication Basic –AllowRedirection

At this point, $Session contains all of the cmdlets needed to work with Exchange Online, but you need to bring them into your local session to use them. Apply the Import-PSSession cmdlet and specify the session you just built. This is called implicit remoting, and essentially imports all of the functionality that exists in $Session into the local session (see figure 3). It creates a temporary PowerShell module that contains all of the Exchange Online commands you can use.

Using PowerShell for Exchange Online.
Figure 3. PowerShell users can import Exchange Online commands to their local session.

Notice the module name in figure 3. This is a temporary module name that contains all of the commands. I recommend documenting this so you can then explore the commands for Exchange Online. In my example, if I run Get-Command –Module tmp_lhg4ygs5.pgh, I will see all of the commands that are available to work with.

Again, run a Get command at first to ensure you're now able to pull information from the service. In this instance, I used Get-AcceptedDomain to ensure my domains show up (see figure 4) for Exchange Online in PowerShell.

Testing PowerShell connection for Exchange Online.
Figure 4. You can test that the service is running correctly by using a Get command, such as Get-AcceptedDomain.

Now that you can connect PowerShell to Office 365 and Exchange Online, explore what you can do. Try to correlate the actions in the graphical user interface with a PowerShell cmdlet. Use the Get-Command cmdlet to discover all of the commands you can use to manage both services. Once you get more comfortable using PowerShell for Office 365 and Exchange Online, you'll find that you'll be able to accomplish tasks much faster at the command line.

Next Steps

Copy files over a PowerShell remote session

Use PowerShell to add mailboxes to Office 365

Master Exchange with PowerShell scripting

This was last published in January 2016



Find more PRO+ content and other member only offers, here.

Essential Guide

The essential admin's guide to Office 365 PowerShell

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

How will you use PowerShell for Exchange Online?