How to Connect to Microsoft 365 Exchange Online With PowerShell
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.
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.
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.
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.
Enter your Microsoft 365 credentials, and then click Enter. You’ll be connected to Microsoft 365 Exchange Online, as shown in Figure 5.
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.
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.
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.
- 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.