In a previous article, I explained how to connect to Office 365 with PowerShell. In this article, we explore how to use PowerShell to connect to Exchange Online. We will cover both the Exchange Online PowerShell Module version 1 and version 2.
Let’s get started!
Exchange Online PowerShell Module v2
One of the challenges of a large Exchange Online organization is that PowerShell commands can take a very long time to run. The Exchange Team released the Exchange Online PowerShell V2 module to combat this problem, which ships with all new Exchange cmdlets while still supporting the old cmdlets. Microsoft has determined that these new cmdlets are up to eight times faster in certain instances. I highly recommend using this module over the V1 module. The V2 module has been generally available since June 2020.
To use the new Exchange Online module, launch PowerShell as an administrator and run the following command.
C:\> Install-Module ExchangeOnlineManagement
If you did not have NuGet previously installed from another PowerShell library you will be prompted to install it. Press Y and hit Enter.
NuGet provider is required to continue. PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\<user>\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y
Once NuGet has installed you will then be prompted to install the Exchange Online Management module. Press Y and hit Enter.
Untrusted repository. You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y
If you have never changed your remote execution policy, I recommend changing it here. This will allow the Exchange Online PowerShell V2 module to run. To do this, type the following and press Y, and hit Enter when prompted.
C:\> Set-ExecutionPolicy RemoteSigned Execution Policy Change. The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y
With all the prerequisites met you can now make a connection to Exchange Online. To do this use the Connect-ExchangeOnline command. You can also pass the -UserPrincipalName parameter to make it easier to reconnect when using the command from the PowerShell buffer.
C:\> Connect-ExchangeOnline -UserPrincipalName <admin account>
Once you have provided your password (and satisfied any MFA challenges) you will be connected to Exchange Online. The initial connection screen gives you example of the new commands and their older counterparts.
------------------------------------------------------------------- The module allows access to all existing remote PowerShell (V1) cmdlets in addition to the 9 new, faster, and more reliable cmdlets. |----------------------------------------------------------------------| | Old Cmdlets | New/Reliable/Faster Cmdlets | |----------------------------------------------------------------------| | Get-CASMailbox | Get-EXOCASMailbox | | Get-Mailbox | Get-EXOMailbox | | Get-MailboxFolderPermission | Get-EXOMailboxFolderPermission | | Get-MailboxFolderStatistics | Get-EXOMailboxFolderStatistics | | Get-MailboxPermission | Get-EXOMailboxPermission | | Get-MailboxStatistics | Get-EXOMailboxStatistics | | Get-MobileDeviceStatistics | Get-EXOMobileDeviceStatistics | | Get-Recipient | Get-EXORecipient | | Get-RecipientPermission | Get-EXORecipientPermission | |----------------------------------------------------------------------|
You can now issue commands.
Exchange Online PowerShell Module v1
If you wish to use the older Exchange Online PowerShell module, log into the Office 365 Admin Center, navigate to the Exchange Admin Center, and click the Hybrid tab.
From the hybrid tab, click the second Configure button (under the text that states The Exchange Online PowerShell Module supports multi-factor authentication).

Note: To download this module, your browser needs to support ClickOnce applications. At the time of writing, ClickOnce is in beta for Edge Chromium. To get ClickOnce in Edge Chromium, type edge://flags/ in the Edge address bar. Toggle the ClickOnce Support dropdown to Enabled.
This will create a new icon on your desktop named Microsoft Exchange Online Powershell Module. Double-click Microsoft Exchange Online Powershell Module.

This will launch PowerShell with the Exchange Online Module preloaded. To connect we will use the Connect-EXOPSSession cmdlet. In this cmdlet swap the -UserPrincipleName parameter for your administrator’s login name.
C:\> Connect-EXOPSSession -UserPrincipalName globaladmin@contoso.com
This command should launch a web pop-up. Enter your Password and click Sign in.

If you have MFA enabled (I hope you do!), you will get a challenge-response. In the example below, the Azure MFA settings are configured to send a push notification to the Microsoft Authenticator app. Other examples include inputting a code received via text message or verifying a phone call. What you see here depends on whether you use Azure MFA or a third-party MFA solution.

Continuing with our Microsoft Authenticator app example, we will click Approve on our mobile device.

You will then be connected to Exchange Online. Be sure to close your session once you are done (Get-PSSession | Remove-PSSession).


How has your experience been with connecting PowerShell to Exchange Online? Or perhaps, connecting to Azure Active Directory? Join the conversation on Twitter @SuperTekBoy.
I am having problems getting the auth method to use Kerberos. Is ADFS absolutely required? If ADFS is not in the mix, can Kerberos Auth work when connecting to EOL via powershell?
Thanks.
Often see this error, it seems i get it 99% of the time. I am using my Global Admin user that works fine on portal.office.com to manage our tenant…
Tried it on 3 separate systems as well
New-PSSession : [outlook.office365.com] Connecting to remote server outlook.office365.com failed with the following
error message :
[ClientAccessServer=BN6PR03CA0024,BackEndServer=,RequestId=f60dc1ae-d3a4-4320-93d2-8e0511694208,TimeStamp=5/11/2017
4:19:02 PM] Access Denied For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:12
+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri ht …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme….RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
gTransportException
+ FullyQualifiedErrorId : -2144108477,PSSessionOpenFailed
had to use my login user as user@domainname.com vs domainname\user
Correct. If using directory synchronization you will want to use your User Principle Name (UPN) when logging into Office 365 not your SAM Account Name.
Thank you very much for this neat explanation. and valuable tips.
Thanks for the post, it was really helpful as I was trying to set up Power Shell to Exchange Online.
One additional step that I had to complete was to allow remote scripts. This was achieved by running the following command
Set-ExecutionPolicy RemoteSigned
then run
Import-PSSession $Session
After this I was able to fully access Exchange
I hope this helps.
Thanks Nobeel. Not sure why I had missed that. Thanks for letting me know.