Connecting to Exchange Online using PowerShell

When dealing with some features of Exchange, we need to use Powershell to perform commands that aren’t available in the Web UI. This article describes how to get ready to run these commands.

I will start by showing how to install the necessary modules, then how to update them and connect to the environment. I will then reference this article in other articles that rely on this functionality.

Opening Powershell

First, we need to start Powershell on the computer. You can do this by right clicking the start menu (Or press the Windows Key on your keyboard and the button X) and choose “Terminal (Admin)”. Sometimes they are called something else depending on you Windows version but they are usually located in the same place in the menu.

Administrative rights aren’t always necessary but since we will be installing sme additional packages, it is necessary.

If you have User Account Control enabled (default in Windows), you will need to confirm that you really want to start the prompt in admin mode:

The Powershell windows will open up and you will see that it says PS (PowerShell) in front of the prompt and you will also see that it says “Adminsitrator:” in the tab name at the top.

Checking if the required modules are already installed

Before installing the module, lets just first check that it isn’t already installed.

Run the command

Get-Module -ListAvailable -Name ExchangeOnlineManagement

Here we can see that it has not been installed

If the module is installed, the output could look something like this:

Installing the necessary modules in Windows

To install the necessary Windows modules, we first need to install some add-ons in Windows Powershell.

The command to run first is

Install-Module -Name ExchangeOnlineManagement -Force

Run the command and confirm that you really want to install the module by pressing “Y” when asked.

Updating the module

The command to update the Exchange module is:

Update-Module ExchangeOnlineManagement

Connecting to your Microsoft 365 Exchange environment

Now that we have installed the necessary module, it’s time to connect to the Exchange environment

The command to initiate the connection is

Connect-ExchangeOnline

You may need to set the Execution Policy to allow script to be run. If you get an error about this, run the command

Set-ExecutionPolicy RemoteSigned

Also, verify that the new setting has been applied:

Get-ExecutionPolicy

When the Connect-ExchangeOnline command has been successfully run, a login prompt will be brought up:

Enter a login with permission to administer your Exchange environment

Enter your password on the next page and confirm with your MFA

If everything was successfull, you should now have a prompt with some information text:

And you are all set to start issue commands!

If you want to list all the mailboxes in your tenant for example, just issue the command

Get-Mailbox

Leave a Reply