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 gives you 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 EXO 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

Managing Exchange Online with PowerShell

You can automate bulk administration tasks, set policies, configure mail flow rules, manage permissions – and many other jobs and manage Exchange Online. Such as:

CmdletDescription
New-MailboxCreates a new mailbox for a user in Exchange Online.
Set-MailboxModifies settings and attributes of an existing mailbox in Exchange Online.
Get-MailboxRetrieves information about mailboxes in Exchange Online.
Remove-MailboxDeletes a mailbox from Exchange Online.

This table provides some of the commonly used Exchange Online PowerShell cmdlets. It highlights the functionality and purpose of each cmdlet, to understand the differences and choose the appropriate cmdlet for the tasks.

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.

Wrapping up

Connecting to Exchange Online PowerShell allows much more control and automation over Office 365 email administration. You can access Exchange Online cmdlets and advanced configuration options by installing the Exchange Online Management module, establishing a remote PowerShell session with administrator credentials, and handling multi-factor authentication. Following the step-by-step instructions in this guide, you will be able to connect from your local machine and manage Exchange Online programmatically.

This opens up many possibilities for batch management tasks, automated workflows, and handling advanced mailbox, group, and mail flow configurations that are more efficient and, in some cases, not possible in the Office 365 admin portal. Learning how to use Exchange Online PowerShell remoting allows administrators to tap into the extensive capabilities that PowerShell provides for cloud-based Exchange administration.

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 - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “How to Connect to Exchange Online using PowerShell?

  • Is it possible to create an Exchange Online Outlook retention policy or PS script for a specific email address? For example, each day my organization sends out a brief company news email. I would like to have the old news emails (not all email, just the news emails) delete in 30 days. Can PowerShell do that?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *