How to Connect PowerShell to Office 365

Adam Bertram

Read more posts by this author.

Connecting to Office 365 with PowerShell can be a pain sometimes. You have to constantly remember different commands and modules. Let’s change that.

In this article, you’ll learn how to connect to Office 365 with PowerShell. You’ll do this by using the Azure AD PowerShell for Graph module and the Azure Active Directory module for Windows PowerShell.

Prerequisites

To run through any of the examples in this tutorial, please be sure you have the following ahead of time:

Why Two Azure AD Modules?

When working with Office 365 using PowerShell, you’ll probably have to use two different modules. You’ll use Azure Active Directory PowerShell for Graph with the module name AzureAD and Azure Active Directory module for Windows PowerShell with the module name of MSOnline.

AzureAD and MSOnline both allow you to work with Office 365 services using PowerShell so why two? AzureAD is the successor to MSOnline and from what I can gather will replace MSOnline at some point. All new functionality is in the AzureAD module but there still is some overlap with the MSOnline module.

You’ll find tasks such as managing users, groups and license administration still exists in the MSOnline module.

Connecting to Office 365 with the AzureAD Module

To connect to Office 355 with PowerShell using the AzureAD module, you’ll first need to install it. You can do so by running Install-Module AzureAD from an administrative PowerShell session.

All nouns for all commands in the AzureAD module are prefixed with AzureAD.

Once you have the module installed, run the Connect-AzureAD cmdlet. Once you do, PowerShell will prompt for your Microsoft ID and password (Work or school account). Once authenticated, you’ll be able to use all of the commands in the module.

Connecting to Office 365 with the MSOnline Module

Connecting to Office 365 with PowerShell using the MSOnline module requires a little bit more effort. Assuming you have the Microsoft Online Services Sign-In Assistant for IT Professionals RTW software package installed:

  1. Install the MSOnline module from the PowerShell Gallery by running from an administrative PowerShell console.
Install-Module MSOnline

2. Once the module is installed, run Get-Module to ensure PowerShell can find the module.

Available MSOnline PowerShell module
Available MSOnline PowerShell module

3. Finally, run the Connect-MsolService cmdlet to authenticate to Azure AD. This will prompt you for your Office 365 credential.

Logging into Azure Active Directory with PowerShell
Logging into Azure Active Directory with PowerShell

Instead of running Connect-MsolService by itself, as shown above, you can also use the Credential parameter. You can do this because you’ve already captured it in the $cred variable. This will prevent the authentication popup box from coming up.

Connecting to Office 365 (Exchange) with PowerShell

If you need to connect to Exchange Online, you must perform one set of steps. You’ll now need to establish a PowerShell implicit Remoting session to make all Exchange Online commands available.

Below you’ll find the PowerShell code to create a credential, build the session and import the commands into your current session.

Related: PowerShell Remoting: The Ultimate Guide

The code snippet below stores your Office 365 username and password in a credential object using the Get-Credential cmdlet. It then uses that credential to establish a PowerShell Remoting session to the Exchange URI and imports the remote session into your local session.

Implicit remoting is the term used to refer to importing remote commands into a local session.

PS> $cred = Get-Credential
PS> $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $cred -Authentication Basic -AllowRedirection
PS> Import-PSSession $Session -DisableNameChecking
Establishing a new PSSession to Exchange Online
Establishing a new PSSession to Exchange Online

If the session import goes well with no error messages, now run Get-AcceptedDomain to ensure your domains show up.

Using the Get-AcceptedDomain cmdlet
Using the Get-AcceptedDomain cmdlet

Once you’re done, disconnect and remove the PSsession by running Remove-PSSession $Session.

If you need to connect to Exchange Online via MFA, Microsoft provides a great guide entitled Connect to Exchange Online PowerShell Using Multi-Factor Authentication.

Summary

Office 365 has many services but fortunately, you can manage them all with PowerShell. Once you’ve installed both modules and connected using the Connect commands, you are on your way to managing Office 365 with PowerShell!

Subscribe to Stay in Touch

Never miss out on your favorite ATA posts and our latest announcements!

Looks like you're offline!