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!
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:
- First, you need to install the Exchange Online PowerShell Module with Install-Module ExchangeOnlineManagement.
- 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.
- 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
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:
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
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.
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@example.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
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:
|Creates a new mailbox for a user in Exchange Online.
|Modifies settings and attributes of an existing mailbox in Exchange Online.
|Retrieves information about mailboxes in Exchange Online.
|Deletes 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
#Connect to Exchange Online
Connect-ExchangeOnline -Credential (Get-Credential) -ShowBanner:$False
#Get All Mailboxes
Get-EXOMailbox | Format-table UserPrincipalName,DisplayName
#Disconnect Exchange Online
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:
- Get Credentials to connect to Exchange Online
- Create a new Microsoft 365 PowerShell session
- Import the new Exchange Online PowerShell session
- Run PowerShell cmdlets for Exchange Online
- 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
#Remove the session
This connects to Exchange Online using remote PowerShell.
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.
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.
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.
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?
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?
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.
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?