Connect to Microsoft 365 with PowerShell
While the Microsoft 365 admin center GUIs are decent, there are many Microsoft 365 management tasks that are better suited for PowerShell. However, before you can start scripting, you must first establish a connection to your Microsoft 365 tenant.
Prerequisites
By default, PowerShell is not configured to run scripts. To fix this, run the Set-ExecutionPolicy
command, from an elevated PowerShell window, and allow execution of scripts signed by a trusted publisher.
Set-ExecutionPolicy RemoteSigned
Instructions
- Connect to Azure Active Directory with AzureAD
- Connect to Azure Active Directory with MSOnline (Deprecated)
- Connect to Exchange Online
- Connect to Exchange Online with EXO V2 (Preview)
Connect to Azure Active Directory (AzureAD)
-
Install the AzureAD module by running the
Install-Module
command from an elevated PowerShell window.Install-Module AzureAD
-
Run the
Connect-AzureAD
command and provide your Microsoft 365 credentials.Connect-AzureAD
Connect to Azure Active Directory (MSOnline)
It is recommended to use the AzureAD module to manage Microsoft 365 users, groups, and licenses. However, the MSOnline is still available if you need functionality that is not yet available in the AzureAD module.
-
Install the MSOnline module by running the
Install-Module
command from an elevated PowerShell window.Install-Module MSOnline
-
Run the
Connect-MsolService
command and provide your Microsoft 365 credentials.Connect-MsolService
Connect to Exchange Online
-
Run the
Get-Credential
command and provide your Microsoft 365 credentials.$UserCredential = Get-Credential
-
Create a new PowerShell session using the credentials provided in Step 1 and import the commands into the current session.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $Session -DisableNameChecking
Connect to Exchange Online (EXO V2)
The Exchange Online PowerShell V2 (EXO V2) module was in Preview at the time of this post.
-
Install the ExchangeOnlineManagement module by running the
Install-Module
command from an elevated PowerShell window.Install-Module PowershellGet -Force Install-Module -Name ExchangeOnlineManagement
-
Run the
Connect-ExchangeOnline
command and provide your Microsoft 365 credentials.Connect-ExchangeOnline