Connect to Microsoft Graph PowerShell 

Connect to Microsoft Graph PowerShell 

We all know that Microsoft is deprecating the Azure AD PowerShell module and MS Online module in 2022. So, admins need to migrate to either Microsoft Graph PowerShell SDK or Microsoft Graph API. 

This blog will guide you on the following things. 

 

Install the Microsoft Graph PowerShell SDK 

You can install Microsoft.Graph module by running the below cmdlet in Windows PowerShell. 

Install-Module Microsoft.Graph -Scope CurrentUser 

It will install the module for the currently logged-in user profile. If you want to install it for all the users, you must run the following cmdlet by opening Windows PowerShell in admin mode. 

Install-Module Microsoft.Graph -Scope AllUsers 

Installing Microsoft.Graph module installs 38 sub-modules. To get a list of available modules, you can use the Find-Module cmdlet. 

Find-Module Microsoft.Graph.* 

 

After installation, you can check the version of the Microsoft Graph PowerShell module by using the following cmdlet. 

Get-InstalledModule Microsoft.Graph 

 

How to Connect to Microsoft Graph PowerShell 

Connect to Microsoft Graph – Things to Know: 

While connecting to Microsoft Graph PowerShell, you need to determine the 2 important things. 

  1. API Version 
  1. Required Scope 

API version: By default, the module uses a stable version of the API. If you want to explore new features, you can use the beta version using the ‘Select-MgProfile’ cmdlet. 

Select-MgProfile –Name “beta” 

Scope: Microsoft Graph provides access to all APIs in Microsoft 365. Each API is protected by one or more permission scopes. The user connecting to Microsoft Graph PowerShell must consent to one of the required scopes. For example, to view all the Microsoft 365 users, you need the ‘User.Read.All’ permission. 

Finding the required scope is challenging. You can refer to the Microsoft doc to determine the required scope. 

Connect to Microsoft Graph: 

The ‘Connect-MgGraph’ cmdlet allows you to connect to Microsoft Graph PowerShell. You will need to sign in with an admin account to consent to the required scopes. 

Connect-MgGraph –Scopes “User.Read.All” 

If you want to connect to Microsoft Graph with multiple scopes, you can provide them as comma- separated values. 

Connect-MgGraph –Scopes “User.Read.All”,”Group.ReadWrite.All” 

Some scopes require you to grant permission on behalf of your organization. Once you connect successfully, you will receive a “Welcome to Microsoft Graph!” message. 

 

Automatically Install and Connect to Microsoft Graph: 

This PowerShell script will automatically install Microsoft.Graph module (if not installed already) upon your confirmation. After installation, it will prompt you to connect to Microsoft Graph. 

Script Download: ConnectMgGraph.ps1

You can execute the script using the following format. 

.\ConnectMgGraph.ps1


Connect-MgGraph

Microsoft graph keeps encrypted token and refreshes it automatically. So, the session remains open for a long time. If you want to connect to a different tenant, you can run the script with the CreateSession parameter. 

.\ConnectMgGraph.ps1 -CreateSession 

 

View Microsoft Graph PowerShell Commands:  

When migrating to a new PowerShell module, it is challenging to find the right cmdlets. For example, to view all the Azure AD users, you need to use the ‘Get-MgUser’ cmdlet. To easily find the required cmdlets, you can follow the below tips.  

To view Microsoft Graph PowerShell cmdlets for a specific module, run the following cmdlet.  

Get-Command -Module Microsoft.Graph.Users  

It will list all the cmdlets related to Azure AD users.  

view Microsoft graph PowerShell commands

As earlier said, you can use Find-Module Microsoft.Graph.* to view the list of modules.  

To view all the Microsoft Graph cmdlets, execute the following cmdlet.  

Get-Command -Module Microsoft.Graph.*  

  

Migrate from Azure AD PowerShell to the Microsoft Graph PowerShell SDK  

As Microsoft is retiring the Azure AD and MSOL module at the end of 2022, admins need to upgrade their current Azure AD scripts. There is no native tool available to convert Azure AD scripts to Microsoft Graph PowerShell. Admins need to upgrade their scripts manually.  

Admins must find the equivalent Microsoft Graph cmdlets for the Azure AD PowerShell cmdlets to convert their scripts. For example, the equivalent of the Get-AzureADUser cmdlet is Get-MgUser. 

 

Disconnect Microsoft Graph PowerShell 

The Microsoft Graph session lasts until you disconnect it. It keeps an encrypted token cache and refreshes it automatically. So, you won’t encounter the ‘Session time out’ error, unlike other PowerShell modules.  

To connect to another tenant, you must disconnect the Microsoft Graph session using the following cmdlet. 

Disconnect-MgGraph  

To avoid using an earlier token cache, you can connect to Microsoft Graph using ‘TenantId’ as below. 

Connect–MgGraph –TenantId <TenantId> 

 

Update and Uninstall Microsoft Graph SDK PowerShell: 

To update the SDK, you can use the following cmdlet. 

Update-Module Microsoft.Graph 

 

If you want to uninstall the Microsoft Graph PowerShell module, you must uninstall the main module first. And then all its dependency modules.  

Uninstall-Module Microsoft.Graph 
Get-InstalledModule Microsoft.Graph.* | %{ if($_.Name -ne "Microsoft.Graph.Authentication"){ Uninstall-Module $_.Name } }  
Uninstall-Module Microsoft.Graph.Authentication 

 

I hope this blog will help you connect to Microsoft Graph PowerShell successfully. We will see how we can do some basic data retrieval using the Microsoft Graph SDK module in our next blog. Happy Scripting! 

Connect to Microsoft Graph PowerShell 

by Kathy time to read: 3 min
0