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 the 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
Otherwise, You’ll encounter “Running scripts is disabled on this system” error! More here: How to Set Execution Policy in PowerShell?

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 for your tenant. Let’s install the Exchange Online V2 PowerShell module. But it’s worth checking if the Exchange Online module is already installed on 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 Exchange Online Management Module
Install-Module -Name ExchangeOnlineManagement -Force

Update Exchange Online PowerShell Module

To update the existing Exchange Online module, use the following:

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 salaudeen@crescent.com

Once 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 PowerShell connection 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.

What is Microsoft Exchange Online used for?

Microsoft Exchange Online is a cloud-based email and calendaring service that is part of the Microsoft Office 365 suite of applications. It allows businesses to securely access their email, contacts, and calendars from anywhere, on any device. It also offers shared calendars, task management, and email archiving features.

How do you connect to Exchange Online with an MFA and PowerShell?

To connect to Exchange Online with MFA and PowerShell, you can use the Connect-ExchangeOnline cmdlet. This cmdlet allows you to create a PowerShell session with both MFA and non-MFA accounts. You will need to enter your Exchange Online credentials and then authenticate with your MFA method. Once you have authenticated, you can run PowerShell scripts to manage Exchange Online.

How do I connect to Azure AD from PowerShell?

To connect to Azure AD using PowerShell, you will need to install the Azure AD PowerShell module and then use the Connect-AzureAD cmdlet. You will need to provide your Azure AD credentials and then you will be able to manage your Azure AD resources using PowerShell.
More info: How to Connect to Azure AD from PowerShell?

How do I access Microsoft Teams in PowerShell?

You can use the Microsoft Teams PowerShell module to access Microsoft Teams in PowerShell. First, you need to install the module by running the following command in PowerShell: Install-Module -Name MicrosoftTeams. Once the module is installed, you can use the Connect-MicrosoftTeams cmdlet to connect to Microsoft Teams with an authenticated account. After executing this cmdlet, you can manage Teams directly from PowerShell.
More info: How to Connect to Microsoft Teams from PowerShell?

How do I run the Exchange Online PowerShell module as an administrator?

To run the Exchange Online PowerShell module as an administrator, you need to right-click on the PowerShell icon and select “Run as Administrator.” Then, you can enter the necessary commands to manage your Exchange Online environment with elevated privileges.

How do I install the Exchange PowerShell module?

To install the Exchange PowerShell module, you can follow these steps: Open PowerShell as an administrator >> Install the Exchange Online Management module by running the following command: Install-Module ExchangeOnlineManagement >> To verify that the module is installed, run the following command: Get-Module ExchangeOnlineManagement -ListAvailable.
More info: How to Install Exchange PowerShell Module?

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. Required fields are marked *