How to Connect to Your Microsoft 365 Tenant Using PowerShell

May 10, 2023
4 min read

Microsoft 365 offers a set of Windows PowerShell modules to install, so that you can start managing your Microsoft 365 tenant right from your Windows machine. This article discusses two modules that help you connect to your Microsoft 365 tenant using PowerShell:

  • Module 1: Connect with Microsoft Azure Active Directory Module for Windows PowerShell (MsolService module)
  • Module 2: Connect with Azure Active Directory PowerShell for Graph Module (AzureAD module)

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

Module 1: Connect with Microsoft Azure Active Directory Module for Windows PowerShell

To connect to your Microsoft 365 tenant using Microsoft Azure Active Directory Module, perform the following steps:

Open your Windows PowerShell terminal (ensure that you are running it as an administrator), and then run the following command:  

Install-Module MSOnline

Figure 1 shows the message that you will get. To continue with the installation, either select:

Y [Yes] 

or

A [Yes to All]

Screenshot of the MsolService PowerShell Module installation message when you install MsolService module in PowerShell. You are asked to confirm whether you to install the modules from PSGallery. Options include Yes, Yes to All, No, No to All, Suspend, and Help
Figure 1: MsolService PowerShell Module installation message. | Used with permission from Microsoft. View Full Size

Once the installation is complete, connect to your Microsoft 365 tenant from Windows PowerShell by typing the following command: 

Connect-MsolService 

A Sign in to your account dialog box should pop up, prompting you to enter your Microsoft 365 admin credentials, as shown in Figure 2. To connect to your Microsoft 365 portal, provide your credentials.

Screenshot of the Sign in page. Connect to your M365 tenant using MsolService module by entering your 365 credentials in the sign in field.
Figure 2: Connecting to your Microsoft 365 tenant using MsolService. | Used with permission from Microsoft. View Full Size

Note: All Microsoft Azure Active Directory Module cmdlets begin with the prefix Msol. If you want to get a list of all the users available in your tenant, the command is:

Get-MsolUser

Module 2: Connect with Azure Active Directory PowerShell for Graph Module

To connect to your Microsoft 365 tenant using the AzureAD PowerShell module, perform the following steps:

Run the following command in your PowerShell terminal: 

Install-Module -Name AzureAD

Figure 3 shows the message that you will get. To continue with the installation, either select:

Y [Yes] 

or

A [Yes to All]

 Screenshot of the AzureAD PowerShell Module Installation message you will get when you install AzureADmodule in PowerShell. You are asked to confirm whether you to install the modules from PSGallery. Options include Yes, Yes to All, No, No to All, Suspend, and Help.
Figure 3: AzureAD PowerShell Module installation message. | Used with permission from Microsoft. View Full Size

Import the installed module so that you can use it by running the following command: 

Import-Module AzureAD

Now you are all set to connect. Run the following command to connect with your Microsoft 365 tenant: 

Connect-AzureAD 

A Sign in to your account dialog box should pop up, prompting you to enter your Microsoft 365 admin credentials. To connect to your Microsoft 365 portal, provide your credentials.

Screenshot of the Sign in page. Connect to your M365 tenant using AzureAD module by entering your 365 credentials in the Sign in field.
Figure 4: Connecting to your M365 tenant using AzureAD. | Used with permission from Microsoft. View Full Size

Note: Microsoft Azure Active Directory PowerShell for Graph Module cmdlets begin with the prefix AzureAD. If you want to get a list of all the users available in your tenant, the command is: 

Get-AzureADUser

Possible Errors You Might Face

Here are examples of errors you might come across: 

  • Not running PowerShell as administrator: You might forget to run Windows PowerShell as an administrator. There are two options to fix this:
    • Run this command: start-process powershell -verb runas
    • Click Start, select PowerShell, and select Run As Administrator.
  • Entering incorrect credentials:  Ensure that you enter your Microsoft 365 credentials.

Conclusion

MsolService and AzureAD modules are popular PowerShell modules that help administrators connect to M365 tenants and manage them efficiently. Installing and using both are similar, with many administrators preferring AzureAD over the MsolService module. Still, certain management tasks can be run using only MsolService. It’s best to install and use both modules and switch between them depending on the task at hand.

For Those Who Like to Watch and Learn

The following video is a shortened version of this article. It explains how you can install MsolService and AzureAD PowerShell modules and use them to connect to your Microsoft 365 tenant.

 

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.