Learn how to connect to Exchange Online PowerShell using multifactor authentication.

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:

  1. Windows PowerShell 5.1 must be installed on your local computer.
  2. The Exchange Online PowerShell module must be installed.
  3. 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:

  1. 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
    Checking the Windows PowerShell version

    Checking the Windows PowerShell version

    If your PowerShell version is lower than 5.1, upgrade to the latest Windows Management Framework.

  2. Install the Exchange Online module. Open an elevated PowerShell window, and run the following command:
    Install-Module -Name ExchangeOnlineManagement
    Installing the ExchangeOnlineManagement module

    Installing the ExchangeOnlineManagement module

    You may be prompted to install the NuGet provider if not installed. Type Y and press Enter to proceed with the installation.

  3. After installing the Exchange Online PowerShell module, you need to import it into your PowerShell session. Run the following command:
    Import-Module ExchangeOnlineManagement
  4. Verify the module installation. List the cmdlets available in the module by running the following command:
    Get-Command -Module ExchangeOnlineManagement
    Viewing the ExchangeOnlineManagement PowerShell cmdlets

    Viewing the ExchangeOnlineManagement PowerShell cmdlets

    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
Connecting to Exchange Online using basic authentication

Connecting to Exchange Online using basic authentication

You will see a message similar to the one below.

Multifactor authentication error connecting to Exchange Online

Multifactor authentication error connecting to Exchange Online

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.

  1. 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

    Beginning modern authentication to Exchange Online PowerShell

    Beginning modern authentication to Exchange Online PowerShell

  2. 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.

    MFA prompt to complete the Exchange Online PowerShell logon

    MFA prompt to complete the Exchange Online PowerShell logon

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:

  1. Get-Mailbox: Retrieves information about all the mailboxes in your Exchange Online environment.
  2. Set-Mailbox: Modifies the properties of a particular user's mailbox.
  3. 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
Running the get mailbox cmdlet in Exchange Online PowerShell

Running the get mailbox cmdlet in Exchange Online PowerShell

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
Disconnecting from Exchange Online PowerShell

Disconnecting from Exchange Online PowerShell

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!

  1. Save your script as a PS1 file on your local computer.
  2. Set the execution policy in PowerShell to allow running scripts. You can do this using the Set-ExecutionPolicy cmdlet:
    Set-ExecutionPolicy RemoteSigned
  3. 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.

avataravatar
2 Comments
  1. Jay 4 months ago

    Is there a way to make PowerShell report to compliance to Conditional Access policies that require compliant devices?

  2. You can connect to other Microsoft services in the same manner e.g., Connect-MsolService, Connect-AzAccount, or Connect-AzureAD

Leave a reply

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

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account