How to Connect to Exchange Online using PowerShell?

Requirement: Install the Exchange Online PowerShell module and Connect to Exchange Online using PowerShell.

PowerShell is the primary administration tool to manage cloud products like Exchange Online in Microsoft 365 suite. The Exchange Online PowerShell module makes the Administrator’s life a lot easier! The cmdlets available in the Exchange Online module let you create/manage/retrieve objects, configure settings that are not available in the Exchange Admin center, manipulate objects, generate reports, etc. This article will show you how to install the Exchange Online Module and connect to the Exchange Online environment from PowerShell. We’ll also walk you through some basics of using PowerShell with Exchange Online. Let’s get started!

Make sure you have the Execution Policy set to “RemoteSigned” before executing the PowerShell scripts. Use: Set-ExecutionPolicy RemoteSigned

How to Install Exchange Online PowerShell Module?

First, we need the Exchange Online PowerShell module that comprises all cmdlets to manage Exchange. Let’s install the Exchange Online V2 PowerShell module. But it’s worth checking if the Exchange Online module is already installed in your client machine!

Get-Module -ListAvailable -Name ExchangeOnlineManagement
check exchange online powershell module installed

This cmdlet gets you the information on whether the Exchange Online PowerShell module is already installed or not. To install the PowerShell module for Exchange Online, Open the Windows PowerShell with the “Run as Administrator” option, and then enter this command:

Install-Module -Name ExchangeOnlineManagement -Force

Update Exchange Online PowerShell Module

To update the existing Exchange Online module, use:

Update-Module ExchangeOnlineManagement

Connect to Exchange Online from PowerShell

Now, we have the module installed. The next step is to connect to your Microsoft 365 Exchange Online with the Connect-ExchangeOnline cmdlet:

#Connect to Exchange Online
Connect-ExchangeOnline

This cmdlet brings the popup to get the credentials (which is MFA aware!) and establishes the connectivity with Exchange Online.

connect-exchangeonline

You can use the “ShowBanner” switch to suppress the banner that displays new cmdlets available in the Exchange Online V2 module. Also, You can supply credentials with the “Credential” parameter.

#Connect to Exchange Online
Connect-ExchangeOnline -Credential $Credential -ShowBanner:$False

Once you are successfully connected to Exchange Online, You can start executing cmdlets for Exchange Online.

Disconnect the Exchange Online PowerShell session

Once you are done with your PowerShell scripts, You should disconnect the session, Instead of just closing the PowerShell window or PowerShell ISE. This way, we can properly disconnect the sessions and avoid waiting for the session to expire on an accidental window close.

#Disconnect Exchange Online
Disconnect-ExchangeOnline -Confirm:$False

Here is an example script to get all mailboxes from Exchange Online:

#Connect to Exchange Online
Connect-ExchangeOnline -Credential (Get-Credential) -ShowBanner:$False

#Get All Mailboxes
Get-EXOMailbox | Format-table UserPrincipalName,DisplayName

#Disconnect Exchange Online
Disconnect-ExchangeOnline -Confirm:$False

The Classic “PSSession” way to connect to Exchange Online

Although it’s obsolete, Here is how to connect to Exchange Online from PowerShell. This method doesn’t need any modules, BTW. This script has Four steps:

  1. Get Credentials to connect to Exchange Online
  2. Create new Microsoft 365 PowerShell session
  3. Import the new Exchange Online PowerShell session
  4. Run PowerShell cmdlets for Exchange Online
  5. Remove the session

To connect to Exchange Online in Office 365, open a PowerShell console, run the following commands:

#Get Credentials to connect
$Cred = Get-Credential

#Establish Exchange session 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
    -Credential $Cred -Authentication Basic -AllowRedirection

#Import the session
Import-PSSession $Session -DisableNameChecking | Out-Null

#Execute cmdlets for Exchange Server
Get-Mailbox
 
#Remove the session
Remove-PSSession $Session

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply