Connecting to Office 365 with PowerShell can be a pain sometimes. You have to constantly remember different commands and modules. 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.
Table of Contents
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.
Azure Active Directory PowerShell for Graph
The AzureAD module can be installed from the PowerShell Gallery using the command
Install-Module AzureAD when run as administrator. All nouns for all commands in this module are prefixed with AzureAD.
To connect to Office 365 using the AzureAD module, simply run the PowerShell command
Connect-AzureAD. You will then be prompted 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.
Azure Active Directory Module for Windows PowerShell
Connecting to Office 365 with PowerShell using the MSOnline module requires a little bit more effort. For this to work, you must first install the Microsoft Online Services Sign-In Assistant.
You’ll then need to install the MSOnline module from the PowerShell Gallery by running
Install-Module MSOnline from an administrative PowerShell console.
Once the module is installed, you can then run
Get-Module to ensure PowerShell can find the module.
Finally, run the
Connect-MsolService to authenticate to Azure AD. This will prompt you for your Office 365 credential.
Connecting to Office 365 (Exchange) with PowerShell
Connecting to Exchange Online requires a second step. You’ll first 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.
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
Instead of running
Connect-MsolServiceby itself as shown above, you can also use the
Credentialparameter. You can do this because you’ve already captured it in the
$credvariable. This will prevent the authentication popup box from coming up.
Get-AcceptedDomain to ensure your domains show up.
Once you’re done, it’s always a good idea to disconnect and remove the PSsession by running
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.
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!