I agree that to connect to Office 365, PowerShell can be a real pain. You have to constantly remember different commands and modules. In this post, we're going to understand connecting to Office 365 with PowerShell with the Azure Active Directory PowerShell for Graph module and the Azure Active Directory module for Windows PowerShell.
Why Two Azure AD Modules
When working with Office/Microsoft 365 using PowerShell, you'll probably have to use two different modules called 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.
I can't speak for Microsoft but it appears that the functionality included with the MSOnline module required so many changes that they introduced the AzureAD module.
At any rate, you're going to need two modules. You can safely use both on the same system.
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. You'll find that 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 software and module are installed, run
Connect-MsolService to authenticate.
Connecting to Exchange Online
Connecting to Exchange Online requires a second step. You'll need to establish a PowerShell Remoting session first and use implicit remoting to make all Exchange Online commands available to you.
Here is the PowerShell code to create the credential to authenticate, build the session and import the commands into your current session. Execute the following commands:
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-MsolService by itself as shown above, you can also use the
Credential parameter at this point since you've always captured it in the
$cred variable like
Connect-MsolService -Credential $cred. This will prevent the authentication popup box from coming 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 or multi-factor authentication, Microsoft provides a great guide entitled Connect to Exchange Online PowerShell Using Multi-Factor Authentication.
Connecting to Azure
On a similar vein, it would be prudent of me to also include connecting to Azure with PowerShell in this post. However, instead of describing that process, I'd just head over to my other post Connect-AzAccount: Your Gateway To Azure with PowerShell where I break down that entire process as well.
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!
Join the Jar Tippers on Patreon
It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!Become a Patron!