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.
Note: This article only covers the PowerShell v2 and v3 modules. Exchange Online PowerShell v1 module was removed from this article as it is deprecated as of October 1st, 2022, with the deprecation of basic authentication.
Let’s get started!
Exchange Online PowerShell Module
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 been 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 examples 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.

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.