Connect to Exchange Online PowerShell 

Connect to Exchange Online PowerShell 

Exchange Online PowerShell helps administrators to manage their Exchange Online Environment more effectively. EXO V2 module allows admins to create remote PowerShell sessions by using both 

  • Modern authentication 
  • Multi-factor authentication 

Let’s see how to install and connect Exchange Online PowerShell using the EXO V2 module. 

Install Exchange Online PowerShell Module: 

To install EXO V2 module, follow the below steps. 

Step 1: Start Windows PowerShell with the “Run as administrator” option.  

Step 2: Run the below cmdlet to install the Exchange Online PowerShell V2 Module (ExchangeOnlineManagement)  

Install-Module –Name ExchangeOnlineManagement 

Note: To identify the installed EXO V2 module’s version, run the below cmdlet. 

Get-Module Name ExchangeOnlineManagement -ListAvailable 

Step 3: After installing the EXO V2 module, import the EXO V2 module cmdlets by running the following cmdlet. 

Import-Module ExchangeOnlineManagement 

EXO V2 module supports REST API-based cmdlets like Get-EXOMailbox, Get-EXOMailboxStatistics, etc. 

 

Connect to Exchange Online PowerShell: 

You can use the Connect-ExchangeOnline cmdlet to create a PowerShell session to Exchange Online. Both MFA and non-MFA accounts can be connected using the below cmdlet. 

Connect-ExchangeOnline 

It will prompt for the user’s name and password. After entering the credential, it will create an Exchange Online PowerShell session. 

 

Automatically Install and Connect to Exchange Online PowerShell: 

To automatically install the EXO V2 module and connect to Exchange Online PowerShell, you can use the below PowerShell script. 

#Check for EXO V2 module installation 
$Module = Get-Module ExchangeOnlineManagement -ListAvailable 
if($Module.count -eq 0)  
 
  Write-Host Exchange Online PowerShell V2 module is not available  -ForegroundColor yellow   
  $Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No  
  if($Confirm -match "[Y]")  
  {  
   Write-host "Installing Exchange Online PowerShell module" 
   Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force 
  }  
  else  
  {  
   Write-Host EXO V2 module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet.  
   Exit 
  } 
} 
Write-Host Connecting to Exchange Online... 
Connect-ExchangeOnline 

 

Common Errors and Troubleshooting Tips: 

1. Set the execution policy to Remote Signed 

Windows PowerShell needs to be configured to run the remote script and it is not enabled by default. If you haven’t enabled it, you will get the below error. 

Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files. 

Solution: Run the following cmdlet. 

Set-ExecutionPolicy RemoteSigned 

 

2. ‘Connect-ExchangeOnline‘ is not recognized as the name of a cmdlet: 

When trying to connect Exchange Online using the ‘Connect-ExchangeOnline” cmdlet, you will get the following error. 

Connect-ExchangeOnline : The term ‘Connect-ExchangeOnline’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 

Solution: To use the Connect-ExchangeOnline cmdlet, you must install and import the ExchangeOnlineManagement module. 

 

3. Disconnect Exchange Online PowerShell session 

Make sure to disconnect the remote PowerShell session when you’re finished. Else you ended up using all remote PowerShell sessions available to you and you will get the following error. 

New-ExoPSSession : Processing data from remote server outlook.office365. com failed with the following error message: [AuthZRequestId=068a9813-8420-43f0-9f20-692228962287] [FailureCategory=AuthZ-AuthorizationException] Fail to create a runspace because you have exceeded the maximum number of connections allowed: 10 

Solution: Disconnect the PowerShell session using below command. 

Disconnect-ExchangeOnline –Confirm:$false 

The above syntax, disconnects the Exchange Online sessions without the prompt. 

 

4. Enable Basic Authentication on the WinRM service: 

EXO V2 module supports modern authentication in Office 365 end. However, it requires basic auth to transport modern auth tokens at the client-side. If WinRM basic authentication is disabled, you will get the error. 

New-ExoPSSession : Connecting to remote server outlook.office365.com failed with the following error message : The WinRM client cannot process the request. Basic authentication is currently disabled in the client configuration. 

Solution: To enable WinRM basic auth, run the below cmdlet in the command prompt. 

winrm set winrm/config/client/auth @{Basic="true"} 

Note: You can use the EXO V2 Preview3 module to connect Exchange Online without WinRM basic auth. 

Connect to Exchange Online PowerShell 

by Kathy time to read: 3 min
0