Connect to Microsoft 365 Using PowerShell

Let’s understand how you can connect to your Microsoft 365 tenant using Windows PowerShell so that you easily automate M365 tenant administration tasks. This article discusses the following:

The articles includes detailed steps including installation and running of commands. Play the GIF to learn visually.

We recommend you use the “Microsoft Graph PowerShell” module for connecting with your Microsoft 365 tenants, as the other two modules (AzureAD and MSOnline) have been deprecated. However, this article discusses them as these two modules will be supported until March 30, 2025.

Points to Remember

To be able to execute the below PowerShell commands, ensure:

  • You are running Windows PowerShell as an administrator. Select Windows Icon >> PowerShell >> Run as Administrator option to run PowerShell as administrator.
  • Your PowerShell execution policy is set to RemoteSigned. Run Get-ExecutionPolicy to check the current execution policy. Run Set-ExecutionPolicy RemoteSigned to set the execution policy to RemoteSigned. (You should confirm your action by selecting Yes to All [A] option as shown in the image).

Connect to your Microsoft 365 subscription using Graph PowerShell module


Installing Microsoft Graph

Install-Module Microsoft.MGGraph -Scope CurrentUser is the PowerShell cmdlet for installing Microsoft Graph PowerShell module. You’ll have to confirm your action – of installing Microsoft Graph (or MGGraph) PowerShell module – by selecting A [yes to all] when prompted during the installation process (as shown in the image).



PowerShell Cmdlet to connect to your Microsoft tenant

Execute Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All" PowerShell cmdlet. Then authenticate yourself and grant (or approve) permissions you need to query the tenant as shown in the image.

This is a click and play gif

Note: Find out more about the different scopes you can pass in to the -Scopes parameter to execute your PowerShell queries by referring to the Microsoft Graph API doc. For example, the scopes you can use to query a specific endpoint or object (like users) gets listed within their reference doc as shown in the image.


Testing Your Connection

Run Get-MGUser cmdlet to test your connection. Running this Microsoft Graph PowerShell cmdlet should get you all the users within your Microsoft 365 tenant as shown in the image (since you passed User.Read.All permission as one of the required scopes) confirming the fact that your connection is successful. Read Microsoft Graph PowerShell documentation for more info on how to connect with Graph PowerShell module.

This is a click and play gif

Connect to Microsoft 365 subscription using AzureAD PowerShell module


Installing AzureAD

Install-Module AzureAD is the PowerShell cmdlet for installing AzureAD module. You’ll have to confirm your action – of installing AzureAD module – by selecting A [yes to all] when prompted during the installation process (as shown in the image).



PowerShell Cmdlet to connect to your Microsoft tenant

This is a click and play gif

Run Connect-AzureAD cmdlet and supply your Microsoft 365 tenant credentials for connecting Windows PowerShell with your Microsoft 365 tenant as shown in the image. You will get back your tenant details like Account name, TenantId, TenantDomain as the response.


Testing Your Connection

Run Get-AzureADUser cmdlet to test your connection. Running this AzureAD cmdlet should get you all the users within your Microsoft 365 tenant as shown in the image, confirming the fact that your connection is successful. Read Microsoft documentation for more info on how to connect with AzureAD module.

This is a click and play gif

Connect to your Microsoft 365 subscription using MSOnline PowerShell module


Installing MSOnline

Install-Module MsOnline is the PowerShell cmdlet for installing MSOnline module. You’ll have to confirm your action – of installing MSOnline module – by selecting A [yes to all] when prompted during the installation process (as shown in the image).



PowerShell Cmdlet to connect to your Microsoft tenant

This is a click and play gif

Run Connect-MsolService cmdlet and supply your Microsoft 365 tenant credentials for connecting Windows PowerShell with your Microsoft 365 tenant as shown in the image.


Testing Your Connection

Run Get-MsolUser cmdlet to test your connection. Running this msonline cmdlet should get you all the users within your Microsoft 365 tenant as shown in the image, confirming the fact that your connection is successful. Read Microsoft documentation for more info on how to connect with MSOnline(MSOL) PowerShell module

This is a click and play gif

Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex