How to Connect to Microsoft 365 Exchange Online With PowerShell

August 3, 2023
5 min read

Windows PowerShell scripting helps Microsoft 365 administrators easily perform Exchange Online management-related activities. Not only do they get to automate stuff, certain tasks like bulk creation of shared mailboxes and recovery of a deleted user mailbox are only possible by using PowerShell.

In this article, let’s understand how you can connect to Microsoft 365 Exchange Online using PowerShell, to manage your Microsoft 365 mailboxes efficiently.

For those who like to watch and learn, check out the 2-minute video at the end of this article, which is a shortened version of this content.

ExchangeOnlineManagement is the PowerShell module that you need to connect with Exchange Online using PowerShell. First, I’ll explain how you can install it. Next, I’ll show how the module connects with your Microsoft 365 Exchange Online.

Installing the ExchangeOnlineManagement PowerShell Module

From your PowerShell console, run the Install-Module ExchangeOnlineManagement command as an administrator, as shown in Figure 1. This installs the Exchange Online management module.

Note: When you install a PowerShell module, you must confirm the installation by entering Y or A as the response, as shown in Figure 1.

This screenshot shows the PowerShell command for installing the exchange online management module.
Figure 1: Install-Module command for installing Exchange Online Module for PowerShell. | Used with permission from Microsoft. View Full Size

Checking Whether the Installation Was Successful

Run the Get-Module ExchangeOnlineManagement command. If your installation was successful, you’ll get back the newly installed ExchangeOnlineManagement module’s details, as shown in Figure 2.

This screenshot shows the PowerShell command for checking whether the exchange online management has been installed successfully.
Figure 2: Running the Get-Module ExchangeOnlineManagement cmdlet to ensure the installation was successful. | Used with permission from Microsoft. View Full Size

If you want to explore all the cmdlets associated with the newly installed ExchangeOnlineManagement PowerShell module, run the Get-command -Module ExchangeOnlineManagement cmdlet, as shown in Figure 3.

This screenshot shows the PowerShell command for listing all the cmdlets associated with the exchange online management module.
Figure 3: Viewing all cmdlets associated with the ExchangeOnlineManagement module. | Used with permission from Microsoft. View Full Size

Connecting to Exchange Online

To connect to Microsoft 365 Exchange Online:

Run the Connect-ExchangeOnline command. This opens the credentials prompt, as shown in Figure 4.

This screenshot shows how you can run the connect exchange online command for connecting with Microsoft 365 exchange online using PowerShell.
Figure 4: Running the Connect-ExchangeOnline cmdlet opens the credentials prompt. | Used with permission from Microsoft. View Full Size

Enter your Microsoft 365 credentials, and then click Enter. You’ll be connected to Microsoft 365 Exchange Online, as shown in Figure 5.

This screenshot shows successful execution of the connect exchange online PowerShell command.
Figure 5: Connecting to Microsoft 365 Exchange Online using PowerShell. | Used with permission from Microsoft. View Full Size

Checking Whether the Connection was Successful

Run the Get-Mailbox command. Doing so will list all the mailboxes available in your Microsoft 365 tenant, as shown in Figure 6.

This screenshot shows how you can run the get mailbox cmdlet to check whether you have successfully connected with Microsoft 365 Exchange Online using PowerShell.
Figure 6: Retrieve all the mailboxes in your Microsoft 365 tenant by using the Get-Mailbox cmdlet. | Used with permission from Microsoft. View Full Size

Disconnecting Exchange Online

It’s best practice to be aware of how you can disconnect from a PowerShell module. The command for disconnecting from Microsoft 365 Exchange Online is Disconnect-ExchangeOnline, as shown in Figure 7.

This screenshot shows how you can run the Disconnect-ExchangeOnline command using PowerShell to disconnect from Microsoft 365 Exchange Online.
Figure 7: Disconnect from Microsoft 365 Exchange Online by using the Disconnect-ExchangeOnline cmdlet. | Used with permission from Microsoft. View Full Size

Possible Errors You Might Face

Here are some errors you might face and ways to rectify them:

  • Not running your shell as administrator: Select the PowerShell program from the Windows start menu, and then choose Run as Administrator, as shown in Figure 8.
This screenshot shows how you can run the PowerShell console as an administrator.
Figure 8: Running PowerShell as administrator. | Used with permission from Microsoft. View Full Size
  • Set the execution policy to restricted mode: The execution policy setting determines the script execution rights for the current user. If you are logging in for the first time, chances are this is set to Restricted mode. Ensure that the execution policy is set to RemoteSigned, which allows you to run scripts. The command for doing so is Set-ExecutionPolicy RemoteSigned

         Note: To check your script execution rights, run the Get-ExecutionPolicy command.

  • Typos in your PowerShell script: You can have typos even when you’re copying/pasting scripts. Save your scripts in separate .ps1 files, and then run them. This not only saves time but also prevents your script from encountering unnecessary errors.
  • Providing incorrect login credentials: Ensure that you get your credentials right to prevent yourself from running into this embarrassing error.

Conclusion

Now that you know how to connect with your Microsoft 365 Exchange Online, in the next article, I’ll show you how to extract useful mailbox-related information from your Microsoft 365 tenant by using PowerShell. Then you’ll see how to use crucial scripts for tasks like creating shared mailboxes in bulk, recovering a deleted user’s mailbox, and others.

For Those Who Like to Watch and Learn

The following video is a shortened version of this article. It shows you how to connect to Microsoft 365 Exchange Online by using PowerShell.

Thilak Kumar Singh

Thilak Kumar Singh

Thilak Kumar Singh is a trainer who believes in teaching about IT in the simplest possible way with more emphasis on visual mode of learning. He firmly believes in democratizing IT knowledge, especially of Microsoft solutions, in which he specializes as a trainer and consultant.

He observes that in this era dominated by cloud computing and collaboration, where IT has reached users unimaginable hitherto, IT knowledge must not be an obscure science, but rather be available to anyone who wishes to engage with IT.