Field Notes

Connect to Microsoft 365 with PowerShell

  • Billy Ford

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 (AzureAD)

  1. Install the AzureAD module by running the Install-Module command from an elevated PowerShell window.

    Install-Module AzureAD
    
  2. 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.

  1. Install the MSOnline module by running the Install-Module command from an elevated PowerShell window.

    Install-Module MSOnline 
    
  2. Run the Connect-MsolService command and provide your Microsoft 365 credentials.

    Connect-MsolService
    

Connect to Exchange Online

  1. Run the Get-Credential command and provide your Microsoft 365 credentials.

    $UserCredential = Get-Credential
    
  2. 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.

  1. Install the ExchangeOnlineManagement module by running the Install-Module command from an elevated PowerShell window.

    Install-Module PowershellGet -Force
    Install-Module -Name ExchangeOnlineManagement
    
  2. Run the Connect-ExchangeOnline command and provide your Microsoft 365 credentials.

    Connect-ExchangeOnline