- Requirements
- Installing the Exchange Online PowerShell module
- Connecting to Exchange Online with basic authentication—deprecated
- Connecting to Exchange Online with multifactor authentication
- Navigating the Exchange Online PowerShell environment
- Handling remote PowerShell sessions
- Executing PowerShell scripts in Exchange Online
- Wrapping up
- Docker logs tail: Troubleshoot Docker containers with real-time logging - Wed, Sep 13 2023
- dsregcmd: Troubleshoot and manage Azure Active Directory (Microsoft Entra ID) joined devices - Thu, Aug 31 2023
- Ten sed command examples - Wed, Aug 23 2023
Requirements
The Exchange Online PowerShell module allows you to perform administrative tasks using PowerShell cmdlets. Before you can connect, you need to meet the following requirements:
- Windows PowerShell 5.1 must be installed on your local computer.
- The Exchange Online PowerShell module must be installed.
- Valid credentials must be available for your Exchange Online organization.
Installing the Exchange Online PowerShell module
Before working with Exchange-related PowerShell environments, install the Exchange Online PowerShell module. This module includes the necessary cmdlets for managing your Exchange Online environment. Follow these steps to install it:
- Check your PowerShell version. The Exchange Online PowerShell module requires PowerShell version 5.1 or later. To check your installed PowerShell version, open a PowerShell window, and run the following command:
$PSVersionTable.PSVersion
If your PowerShell version is lower than 5.1, upgrade to the latest Windows Management Framework.
- Install the Exchange Online module. Open an elevated PowerShell window, and run the following command:
Install-Module -Name ExchangeOnlineManagement
You may be prompted to install the NuGet provider if not installed. Type Y and press Enter to proceed with the installation.
- After installing the Exchange Online PowerShell module, you need to import it into your PowerShell session. Run the following command:
Import-Module ExchangeOnlineManagement - Verify the module installation. List the cmdlets available in the module by running the following command:
Get-Command -Module ExchangeOnlineManagement
If the cmdlets are displayed correctly, your installation and import were successful.
Now that you've installed the Exchange Online PowerShell module, you can connect to Exchange Online using the Connect-ExchangeOnline cmdlet and manage your Exchange Online environment with the available cmdlets.
Connecting to Exchange Online with basic authentication—deprecated
While you used to be able to connect to Exchange Online PowerShell using basic authentication, Microsoft has deprecated this capability. As a result, Microsoft has removed the ability to use basic authentication in Exchange Online for Exchange ActiveSync, POP, IMAP, Remote PowerShell, Exchange Web Services, Offline Address Book, Autodiscover, Outlook for Windows, and Outlook for Mac.
See the official Microsoft documentation for more information.
You can see the behavior when you attempt to use basic authentication to authenticate to Exchange Online:
$UserCredential = Get-Credential Connect-ExchangeOnline -Credential $UserCredential
You will see a message similar to the one below.
Connecting to Exchange Online with multifactor authentication
Modern authentication offers increased security by incorporating multifactor authentication (MFA) into the logon process and is now the standard best practice for securing logon credentials in most solutions.
To connect to Exchange Online using modern authentication, follow these steps.
- Open a Windows PowerShell window. Run the Connect-ExchangeOnline cmdlet without specifying any credentials. This action will prompt a sign-in window, where you'll enter your user principal name.
Connect-ExchangeOnline
- After entering your credentials, you will be prompted for your MFA code. Enter the verification code to complete the authentication process and establish a connection with Exchange Online.
Navigating the Exchange Online PowerShell environment
Once connected to Exchange Online PowerShell, you can leverage various cmdlets to manage your organization. Some common Exchange Online PowerShell cmdlets include:
- Get-Mailbox: Retrieves information about all the mailboxes in your Exchange Online environment.
- Set-Mailbox: Modifies the properties of a particular user's mailbox.
- Get-Recipient: Gathers information about recipients in your Exchange Online organization.
To execute a specific cmdlet, simply type it in the PowerShell window, and press Enter. For example:
Get-Mailbox
Handling remote PowerShell sessions
When you connect to Exchange Online PowerShell, a remote PowerShell session is established between your local computer and the Exchange Online environment. Therefore, managing these remote PowerShell sessions is essential, especially when working with multiple sessions simultaneously.
To view all active remote PowerShell sessions, use the Get-PSSession cmdlet:
Get-PSSession
To disconnect from a remote PowerShell session, use the Disconnect-ExchangeOnline cmdlet:
Disconnect-ExchangeOnline
Executing PowerShell scripts in Exchange Online
PowerShell scripts can be a powerful tool for managing Exchange Online environments. To run a PowerShell script in Exchange Online, do the following:
Subscribe to 4sysops newsletter!
- Save your script as a PS1 file on your local computer.
- Set the execution policy in PowerShell to allow running scripts. You can do this using the Set-ExecutionPolicy cmdlet:
Set-ExecutionPolicy RemoteSigned - Execute the script using the & operator followed by the script's file path:
& "C:\ScriptPath\Script.ps1"
You may receive a UAC prompt for elevated permissions depending on the tasks performed by the script.
Wrapping up
Using the Exchange Online PowerShell module is an excellent way to perform bulk tasks very quickly and in an automated manner. After installing the Exchange Online PowerShell module, connecting to the Exchange Online environment using PowerShell is supported using modern authentication. Basic authentication has now been deprecated and is no longer available.
Is there a way to make PowerShell report to compliance to Conditional Access policies that require compliant devices?
You can connect to other Microsoft services in the same manner e.g., Connect-MsolService, Connect-AzAccount, or Connect-AzureAD