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 default Execution Policy set to “RemoteSigned” before executing the PowerShell scripts. Use: Set-ExecutionPolicy RemoteSigned

Connecting to Exchange Online from PowerShell – Overview

Exchange Online is a cloud-based email service that is part of Microsoft Office 365. Connecting to Exchange Online using PowerShell gives you the ability to manage your Exchange Online environment using a single, consistent interface. To connect to Exchange Online using PowerShell:

  1. First, you need to install the Exchange Online PowerShell Module with Install-Module ExchangeOnlineManagement.
  2. Once the module is installed, you can connect to Exchange Online using the Connect-ExchangeOnline cmdlet. This cmdlet will prompt for your Microsoft 365 Exchange admin credentials.
  3. Once credentials are entered, You will be connected to the Exchange Online environment. From there, you can use any of the cmdlets that are available in the Exchange Online PowerShell Module to manage Exchange Online settings and objects such as mailboxes, calendars, and contacts.

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 modern authentication popup to get the credentials (which is Multi-factor authentication – 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 (The ShowProgress:$false has no effects as of now, BTW!). Also, You can supply the username and password with the “Credential” parameter.

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

#You can also connect by a particular user
#Connect-ExchangeOnline -UserPrincipalName [email protected]

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:

#Import PowerShell module for Exchange Online
Import-Module ExchangeOnlineManagement

#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

Get All Exchange Online Cmdlets

To get a list of all available Exchange Online PowerShell cmdlets, use:

Get-command -Module ExchangeOnlineManagement

The Classic “PSSession” way to connect to Exchange Online

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

  1. Get Credentials to connect to Exchange Online
  2. Create a 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, and 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

This connects to Exchange Online using remote PowerShell.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published.