Connecting to Office 365 with PowerShell can be a pain sometimes. 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 module is installed, you can then run
Get-Module to ensure PowerShell can find the module.
Finally, run the
Connect-MsolService to authenticate. 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 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-MsolServiceby itself as shown above, you can also use the
Credentialparameter at this point since you've always captured it in the
Connect-MsolService -Credential $cred. 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 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!
Subscribe to Adam the Automator
Get the latest posts delivered right to your inbox