Exchange Online PowerShell Cmdlets are always handy for us as the Exchange Admin Center and Office 365 Admin Center has become less capable of doing many tasks.
Many useful administration tasks can be done using PowerShell cmdlets that are not available in Exchange Admin Center. And also, if you need more customization on results or tired of manually doing repetitive tasks, you should go with PowerShell.
Feb 2023 Update: Remote PowerShell (RPS) retirement in Exchange Online PowerShell
Due to RPS retirement, you can’t use EXO V1 and V2 modules to connect Exchange Online PowerShell. So admins must install/update to EXO V3 module to perform Exchange management tasks.
Connect to Exchange Online PowerShell:
Here is the short code that create PowerShell session to Exchange Online.
1 2 3 |
$Credential=Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection Import-PSSession $Session -AllowClobber |
Note: The above cmdlets use basic authentication to create Exchange session. Since Microsoft deprecating basic authentication in Exchange Online, you can use Exchange Online PowerShell V2 module to connect Exchange Online PowerShell with Modern Authentication.
Connect to Exchange Online PowerShell with MFA:
If the admin account has MFA enabled, you can’t connect to Exchange Online using above mentioned normal cmdlets. You should use Connect-EXOPSSession.
1 |
Connect-EXOPSSession -UserPrincipalName <UPN> |
Warning: Above cmdlet may look simple, but it requires to install Microsoft’s “Exchange Online Remote PowerShell Module”.
We have written an easy to use script to simplify the installation of Exchange Online Remote PowerShell Module and Connection to Exchange Online PowerShell. Also, if you want to do it manually, we have steps for that.
- Automated method: PowerShell script to connect Exchange Online PowerShell (Works for MFA too)
- Manual method: Connect to Exchange Online PowerShell
Automated Method: PowerShell Script to Connect Exchange Online PowerShell (Works for MFA too)
We have developed two in one PowerShell script which helps you to connect to Exchange Online PowerShell using both normal and MFA enabled account. You can use this in your existing scripts or use in your future Exchange Online scripts.
Download Script: ConnectExchangeOnlinePowerShell.ps1
Step1: Download the above script
Step2: To connect Exchange Online with MFA, run the below cmdlet
1 |
./ConnectExchangeOnlinePowerShell.ps1 -MFA |
Note: If the Exchange Online Remote PowerShell Module is not installed already, the script will download and then install (after getting confirmation) the module.You can also download and install Exchange Online Remote PowerShell module manually.
To connect Exchange Online with non-MFA account, run the below cmdlet
1 |
./ConnectExchangeOnlinePowerShell.ps1 |
After providing credential in the popup, you can access Exchange Online from the PowerShell.
Use cases of this script:
1.You can pass the credential explicitly as a parameter. This way, you don’t need to enter the credential in the popup. This will be useful when you automate/schedule PowerShell script.
1 |
./ConnectExchangeOnlinePowerShell.ps1 -UserName Admin@Contoso.com -Password XXXX |
If the UserName/Password has blank space, then it should be enclosed with quotes.
2.To disconnect PowerShell session, use -Disconnect param.
1 |
./ConnectExchangeOnlinePowerShell.ps1 –Disconnect |
Let’s see how to manually connect Exchange online PowerShell and tips to troubleshoot common errors
Manual Method: Connect to Exchange Online PowerShell
1. Open Windows PowerShell as Administrator.
2. Run the below command and it will prompt to enter Office 365 username and password.
1 |
$Credential=Get-Credential |
3. Now, we create a PowerShell session to Exchange Online using New-PSSession cmdlet
1 |
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection |
4. After creating session, you need to import Exchange Online cmdlets to current session that can be done by Import-PSSession cmdlet.
1 |
Import-PSSession $Session |
Now Exchange Online cmdlets are imported into your local Windows PowerShell session and it can be tracked by a progress bar.
If you don’t receive any errors, you connected successfully. If you successfully connected, Exchange Online PowerShell looks similar to below screenshot.
To check the connectivity, you can run Get-Mailbox cmdlet and see the results
This one word can connect to all Office 365 Service PowerShell (Supports MFA too)!!
If you want to connect all Office 365 services like AzureAD, ExchangeOnline,SharePoint Online,Security and Compliance Center,Skype, and Teams via PowerShell, refer: Connect all Office 365 service using PowerShell (Supports MFA too)
Connect to Exchange Online PowerShell – Basic Troubleshooting Tips
1.Set execution policy
Windows PowerShell needs to be configured to run scripts, and by default, it isn’t. In that case, you will get the following error.
Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files.
To resolve this error, you need to run the below cmd before executing any ps file.
1 |
Set-ExecutionPolicy RemoteSigned |
2.You can’t use Standard Windows PowerShell to Connect Exchange Online With MFA
If you use standard Windows PowerShell to connect to Exchange Online PowerShell using MFA enabled account, you will get the following error.
New-PSSession : outlook.office365. com Connecting to remote server outlook. office365. com failed with the following error message: Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange -Conne ... + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed
Import-PSSession : Cannot validate argument on parameter 'Session'. The argument is null. Provide a valid value for the argument, and then try running the command again. + Import-PSSession $Session -CommandName Get-Mailbox,Get-MailboxPermi ... + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands. ImportPSSessionCommand
Solution: You need to install “Exchange Online PowerShell Module” (EXO).
3.Disconnect the remote PowerShell session
Make sure to disconnect the remote PowerShell session when you’re finished. Else you ended up using all remote PowerShell sessions available to you and you will the following error.
New-PSSession : [outlook.office365. com] Processing data from remote server outlook.office365. com failed with the following error message: Fail to create a runspace because you have exceeded the maximum number of connections allowed: 10 for the policy party: MaxConcurrency. Please close existing runspace and try again.
To disconnect all the PowerShell session in the current window, run the below command.
1 |
Get-PSSession | Remove-PSSession |
For more trouble shooting tips, you can refer Exchange Online PowerShell-Troubleshooting tips
Connect to Exchange Online PowerShell without Basic Authentication:
As earlier stated, Microsoft is planning to deprecate basic authentication in Exchange Online this year. So, how will you connect to EXO? You can use either one of the below methods to connect Exchange Online PowerShell with modern authentication.
- Connect to Exchange Online using Exchange Online Remote PowerShell module
- Connect to Exchange Online PowerShell using EXO V2 module
Challenges in Generating Reports with PowerShell
Most admins use Exchange Online PowerShell for generating Exchange online reports and auditing Office 365 environment. But I have seen a lot of challenges when using PowerShell. for example,
- If you do not retrieve the audit data properly, it will end up with data loss which spoils the purpose.
- If you are a newbie, you might lose in search of finding the right cmdlet.
- PowerShell requires a lot of effort to generate the needed reports, which is time-consuming.
- Automating report generation is difficult when you are using MFA.
Have you ever wanted to manage your Exchange Online environment without PowerShell? If your answer is YES, then try AdminDroid Exchange Online reporting and auditing tool. This tool offers 170+ reports on mailbox info, mailbox usage, email activity, mailbox permission, mailbox auditing, etc.
Additionally, AdminDroid provides 1500+ pre-built reports to manage all the Office 365 services like Azure AD, Exchange Online, SharePoint Online, Microsoft Teams, OneDrive for Business, One Note, Stream, etc., with few mouse clicks.
Besides, AdminDroid offers 100+ reports and a handful of dashboards completely for free. It includes reports on Users, Licenses, Groups, Group Members, Devices, Login Activities, Password Changes, License Changes, and more. The free edition doesn’t have any restrictions in reporting functionalities such as customization, scheduling, and exporting. Download Free Office 365 reporting tool by AdminDroid and see how it helps for you.
I hope this blog can help you in managing Exchange Online environment. If you face any other challenges in Office 365 Environment or need of any PowerShell scripts related to Office 365, let us know in the comment section. Happy Scripting!