How to connect to Microsoft 365 Online Exchange using PowerShell

PowerShell is a very powerful tool to manage Microsoft Exchange. There are a few ways to connect to MS Online Exchange using Powershell. We will discuss a few of them. The first two processes support MFA.

Table of Contents

Connecting using Exchange Online Powershell V2 (EXO V2) Module

All versions of the EXO V2 module are supported in Windows PowerShell 5.1. PowerShell 7 on Windows requires version 2.0.4 or later of the EXO V2 module.  And all the versions of the EXO V2 module support MFA.  To download the module we need to allow PowerShell to run scripts. To do so, we will run the following commands:

				
					Set-ExecutionPolicy RemoteSigned
Get-ExecutionPolicy

				
			

In my case, I have set it to unrestricted as I regularly run unsigned scripts.

While installing the module if you get the error WinRM cannot process the request or session is failed using OAuth then run the following command in PowerShell in elevated mode. It will allow basic authentication for WinRM.

				
					Set-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\WinRM\Client' -Name 'AllowBasic' -Type DWord -Value '1'
				
			

To install the module we will run the following command and then check the version installed. To update the future version we will run the update module command.

				
					Install-Module -Name ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement; Get-Module ExchangeOnlineManagement
Update-Module -Name ExchangeOnlineManagement

				
			

Once installed we can connect to Exchange Online using the following command and then provide credentials and follow the steps. Once completed we are connected to Online Exchange.

				
					Connect-ExchangeOnline
				
			

Connecting using PowerShell module downloaded from Exchange Admin Center

Another way of connecting to Exchange Online is by downloading and installing the required module from the admin portal. This module also supports Multifactor authentication.

Login to Microsoft portal  in Microsoft Edge using the admin credential and navigate to the Exchange admin center and click on the hybrid.

Download the Microsoft.Online.CSE.PSModule.Client and install it.

Launch the application – Microsoft Exchange Online PowerShell Module and connect using the following command –

				
					Connect-EXOPSSession -UserPrincipalName <your upn> 
Connect-EXOPSSession -UserPrincipalName aditya@sutaantra.com

				
			

Provide the password and the code and connect. In this case, this is a code on the registered mobile number but it can be other also like – verification through Microsoft authenticator app.

Connecting with PowerShell without MFA

Though nobody should use administrator login without MFA if you still need it, then we can connect using PowerShell without installing any module or application. To do that, open PowerShell in administrator mode and type the below command, and type Y.

				
					Set-ExecutionPolicy RemoteSigned 
				
			

Type the below command and provide the credential

				
					$UserCredential = Get-Credential
				
			

Then type the below to start a session

				
					$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
				
			

Then start the session

				
					Import-PSSession $Session
				
			

To close the session type the following command.

				
					Remove-PSSession $Session
				
			
Check Our

Related Posts