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.
To be able to execute the below PowerShell commands, ensure:
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).
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.
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.
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.
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).
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.
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.
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).
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.
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
© m365corner.com. All Rights Reserved. Design by HTML Codex