Connect to Exchange Online PowerShell

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.

 

Connect Exchange Online PowerShell:

Here is the short code that create PowerShell session to Exchange Online.

 

Connect Exchange Online PowerShell with MFA:

If the admin account has MFA enabled, you can’t connect Exchange Online using above mentioned normal cmdlets. You should use Connect-EXOPSSession.

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)

We have developed two in one PowerShell script which helps you to connect Exchange Online PowerShell using both normal and MFA enabled account. To ease you work, we have developed it as reusable PowerShell function. So that, you can use this in your existing scripts or use in your future Exchange Online scripts.

 

You can Download the PowerShell script from TechNet Gallery.

 

Step1: To connect Exchange Online using script, download the above script (Note:Extention is psm1 as it is a PowerShell module file) and then run the below cmdlet

Import-Module <Path to the PowerShell Module>

For eg,

 

Step2: To connect Exchange Online with MFA, run the below cmdlet

Note: If the Exchange Online Remote PowerShell Module is not installed already, the script will download and then install (after getting confirmation) the module. If you want to download and install manually, you can refer Connect Exchange Online PowerShell with MFA.

To connect Exchange Online with non-MFA account, run the below cmdlet

After providing credential in the popup, you can access Exchange Online from the PowerShell.

Note: You can use the above cmdlets directly in PowerShell window or your script. While using in the script, keep psm1 file in the same folder of your script.

 

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.

If the UserName/Password has blank space, then it should be enclosed with quotes.

2.To disconnect PowerShell session, use -Disconnect param.

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.

Connect Exchange Online PowerShell

3. Now, we create a PowerShell session to Exchange Online using New-PSSession cmdlet

4. After creating session, you need to import Exchange Online cmdlets to current session that can be done by Import-PSSession cmdlet.

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

 

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

PowerShell script to Connect Exchange Online PowerShell

 

2.You can’t use Standard Windows PowerShell to Connect Exchange Online With MFA

If you use standard Windows PowerShell to connect Exchange Online 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

Connect Exchange Online With MFA

If you want to connect Exchange Online PowerShell with MFA, 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.

For more trouble shooting tips, you can refer Connect Exchange Online PowerShell-Troubleshooting tips

 

If you need more PowerShell scripts on Office 365/Exchange Online, you can visit https://o365reports.com.

 

I hope this blog can help you to create a PowerShell session to Exchange Online. If you face any issues during connection, share with us through the comment section.

Even if you face any other challenges in Office 365 Environment or in need of any PowerShell scripts related to Office 365, let us know in the comment section. Happy Scripting!