How to Connect Office 365 Exchange Online Services to PowerShell

More often than not, Microsoft Office 365 and all included applications are managed via a web browser in a graphical user interface. Basically, you use Office 365 Admin Center and Exchange Admin Center web interfaces. However, you may need to use the command line interface (CLI) to perform bulk actions that apply to hundreds of user accounts. In addition, you may want to use the Exchange Online cmdlets, which are not available in the Admin Center.

Many administrators use PowerShell (provided by Microsoft) to manage Microsoft Exchange Server in the command line interface. However, these standard commands for on-premises Exchange Server environments don’t work for Exchange Online running in the Microsoft 365 cloud. To use Powershell with Office 365, you need to install special PowerShell modules to connect to Office 365. Read this post to learn about the different methods to connect to Exchange Online PowerShell.

Backup for Microsoft 365 Data

Backup for Microsoft 365 Data

Use the NAKIVO solution to back up Microsoft 365 data in Exchange Online, Teams, OneDrive and SharePoint Online for uninterrupted workflows and zero downtime.


There are some requirements that must be met to connect to Exchange Online PowerShell.

  • You should use PowerShell on Windows 7 SP1, or newer desktop Windows versions and Windows Server 2008 R2 SP1, or newer server Windows versions. Be aware that you need to install .NET Framework 4.5 or later in addition to installing an updated version of Windows Management Framework 3.0, 4.0, or 5.1.
  • An internet connection is required. TCP port 80 must be opened to connect from your local machine to the destination host.
  • Access to Exchange Online PowerShell must be enabled for the current user (by default such access is enabled for administrators).

You can manually enable access to connect to Exchange Online PowerShell for the particular user with the command:

Set-User -Identity -RemotePowerShellEnabled $true

Working Principle

You can connect to Exchange Online PowerShell, but this process is more complicated than using PowerShell for managing a local Exchange Server. However, you can use the built-in PowerShell console to manage remote cloud infrastructures. In this case, the console is called remote PowerShell or PowerShell Remoting. The process of initiating a remote PowerShell session for Office 365 and Exchange Online is slightly different. You should download and install special components before you can open a remote Office 365 session. Fortunately, the cmdlets required to initiate a remote Exchange Online PowerShell are downloaded automatically when you create a remote PowerShell session. Different sets of PowerShell cmdlets are used to manage Microsoft Office 365 and Microsoft Exchange Online.

The main reasoning behind connecting to Microsoft Exchange Online in PowerShell entails the following:

  • Creating a remote session to Exchange Online in PowerShell opened on your local machine.
  • Providing connection settings, passing authentication.
  • Importing PowerShell cmdlets that are needed to manage Exchange Online remotely.

In today’s blog post, we will run PowerShell cmdlets on Windows 10.

Manual Configuration

Let’s review the manual method first, to understand the configuration principle better.

  1. Open Windows PowerShell. You can do this with at least two methods.
    1. Click Start, type cmd, right click the Command Prompt item and select Run as Administrator in the context menu.
    2. Go to Start > Windows PowerShell. Right click Windows PowerShell, and hit Run as Administrator to make sure that you can run PowerShell commands without restrictions.
  2. Enable running scripts (it is better to run this command in the beginning of preparing PowerShell to manage Exchange Online and Office 365), otherwise you will get the error in future when running the Import-PSSession command:

    Import-PSSession : Files cannot be loaded since running scripts has been disabled on this system. Provide a valid certificate with which to sign the files.

  3. In order to execute scripts, the execution policy must be set to RemoteSigned.

    Set-ExecutionPolicy RemoteSigned

  4. Press Y to confirm changing the policy if prompted. You can also use the Set-ExecutionPolicy Unrestricted command to use the Unrestricted policy.

    By default, the execution policy mode is Restricted.Change the execution policy before you connect to Exchange Online PowerShell

  5. Run the command in PowerShell to get credentials and enter your administrator login/password in the popup window to access Exchange Online. The user must have global administrative permissions in Office 365.

    $Credential=Get-CredentialHow to connect to Office 365 PowerShell – saving credentials as the variable
    The entered credentials will be saved in the variable and used in the next command as $Credential.

  6. You have to create a remote PowerShell session with the New-PSSession cmdlet and running the following command:

    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $Credential -Authentication Basic -AllowRedirection

    Notice that in this command, the target URL of the Exchange Online server running in the cloud that must accept the request is set. After running the command, Microsoft Office 365 cloud servers will provide you access to the appropriate Exchange Online virtual server associated with your account.How to connect to Exchange Online PowerShell – creating a new remote session

  7. Exchange Online PowerShell cmdlets must be imported to the current session with the command:

    Import-PSSession $Session

  8. You can see the progress bar while receiving the commands.How to connect to Exchange Online PowerShell – importing Exchange online cmdlets to the current session
  9. After successfully running the command, you will see the following message.How to connect to Exchange Online PowerShell – Exchange Online cmdlets have been imported successfully
    If you use the MFA for your account, the standard cmdlets explained above will not work. If you want to connect Exchange Online in PowerShell using MFA, run the following command:

    Connect-EXOPSSession -UserPrincipalName YOUR_UPN

    Where YOUR_UPN (user principle name) is the name of the Office 365 account you are using.You may need to install Microsoft’s Exchange Online Remote PowerShell Module. Be aware that when using this module, the session ends after one hour, which may be inconvenient for running long scripts. Consider using Trusted IP addresses (i.e. the IP addresses of your organization) to bypass MFA when connecting from the network of your company to Exchange Online PowerShell.MFA (Multi-Factor-Authentication) is the advanced method of authentication that adds a second layer of security. After entering a password, the confirmation code is sent to the user’s cell phone and the user must enter the confirmation code to verify the account to get access to Office 365 cloud services.

  10. Once you have connected to Office 365 and Exchange Online, you can manage your Office 365 cloud environment. Let’s verify that we have connected to Exchange Online correctly and list the mailboxes of users, for example.

    Exchange Online PowerShell – checking mailboxes of users
    You can list all cmdlets available for Exchange Online PowerShell with the following command:

    Get-Command -Module tmp*

    The names of Exchange Online PowerShell cmdlets are not converted.

  11. When you end your work with Exchange 365, disconnect the session. This is the recommended practice.

    Remove-PSSession $Session

    Unfortunately, no messages are displayed after executing this command. You can check whether the session is disconnected by running the Get-MailBox command. If the session is disconnected, you will get the error explaining that you cannot run Exchange Online cmdlets after disconnecting.

Why should you disconnect the session? Well, simply because the number of active concurrent sessions that can be opened simultaneously is limited to three. If you open three Exchange Online PowerShell sessions at once and don’t disconnect any of them when not in use, you will need to wait until one of these sessions expires before you can connect to Exchange Online PowerShell again from a new PowerShell console.

Automated Configuration

Now that you know the principle of how to connect to Exchange Online PowerShell manually, you can use the automated method. The advantage of this method is the lower number of commands you should enter.

  1. Download the script from the Microsoft’s web site. The name of the script file is ConnectExchangeOnlinePowerShell.ps1 in this case.
  2. Go to the directory where the script is located; in our example, the script is saved to C:\temp_win\.
  3. Before running the script, edit the script execution policy (similarly to what is shown in the first method), otherwise you will get the error:

    The file C:\temp_win\ConnectExchangeOnlinePowerShell.ps1 is not digitally signed. You cannot run this script on the current system.

    You can apply the Bypass execution policy to avoid this issue:

    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

  4. Type Y to confirm the execution policy change.Connect to Exchange Online PowerShell by using a PowerShell script
  5. After that, you can run the script. If you don’t use MFA, run the script without additional arguments:


    If MFA (Multi-Factor Authentication) is used in your Office 365 environment, try the command:

    .\ConnectExchangeOnlinePowerShell.ps1 -MFA

  6. Now that you have successfully connected to Exchange Online, you can manage your user accounts, their mailboxes, etc. For example, you can list the mailboxes of your users:

    Exchange Online PowerShell is now connected – checking mailboxes
    This script can be used to schedule and automate tasks. For example, you can connect to Exchange Online without entering credentials in the interactive window as shown above. You can enter your login and password in the command line as command options when executing the script:

    ./ConnectExchangeOnlinePowerShell.ps1 -UserName -Password your_password

    Keep in mind that entering passwords as plain text in the command line may be not secure.

  7. When you finish to work with Exchange Online in PowerShell, don’t forget to end the session:

    ./ConnectExchangeOnlinePowerShell.ps1 -Disconnect

Alternative Method

Let’s consider one more method that can be used to connect to Exchange Online PowerShell. This method can be considered as a modification of the first method.

  1. Create a new profile for PowerShell with the function:

    New-item -type file -force $profile

  2. Edit the profile configuration file in the text editor to add the function titled Connect-EXOnline:

    notepad $profile

  3. Add the following content to the PowerShell profile configuration file and change to your account name, then save the text file.

    Function Connect-EXOnline
    $credentials = Get-Credential -Credential
    Write-Output “Getting the Exchange Online cmdlets”
    $Session = New-PSSession -ConnectionUri `
    -ConfigurationName Microsoft.Exchange -Credential $credentials `
    -Authentication Basic -AllowRedirection
    Import-PSSession $Session

  4. Close the current PowerShell window and open a new PowerShell window as Administrator. Run the command to connect to Exchange Online PowerShell:

    Enter your password in the popup window.PowerShell connect to Exchange Online

  5. When you have finished working with Exchange Online PowerShell, end the session with the command:

    Get-PSSession | Remove-PSSession


Exchange Online PowerShell is a nice alternative to the Exchange Admin Center web interface. With Office 365 PowerShell and Exchange Online PowerShell, you can perform bulk operations and actions with multiple objects by using a single command or script. Today’s blog post has covered how to connect to Exchange Online PowerShell by using three methods, one of which is automated. The working principle of each method is similar and consists of three main steps: create a remote PowerShell session, authenticate, and import Exchange Online PowerShell cmdlets.

An important recommendation is that you disconnect the remote PowerShell session when you finish working with Exchange Online PowerShell to avoid a situation when all sessions are busy and opening a new remote Exchange Online PowerShell session is not possible. Having Exchange Server in the cloud is a reliable solution, but even in this case, it is recommended that you make a backup of your Exchange server to protect your data against incidental deletion and other disasters.

1 Year of Free Data Protection: NAKIVO Backup & Replication

1 Year of Free Data Protection: NAKIVO Backup & Replication

Deploy in 2 minutes and protect virtual, cloud, physical and SaaS data. Backup, replication, instant recovery options.

People also read