Skip to content

Install Microsoft Graph PowerShell module

Microsoft Graph PowerShell replaces the Azure AD PowerShell and MSOnline modules and is recommended for interacting with Azure AD. The Microsoft Graph PowerShell module is what you need to use when connecting to Azure AD. In this article, you will learn how to install Microsoft Graph PowerShell module.

Microsoft Graph PowerShell module

The Microsoft Graph PowerShell SDK is made up of a set of modules that enable you to interact with the Microsoft Graph API using PowerShell commands. The modules consist of commands that act as wrappers for the API, allowing you to access all the features and functionality of the API through PowerShell.

The Microsoft Graph PowerShell modules are:

  1. Microsoft Graph
  2. Microsoft Graph Beta

The Microsoft Graph PowerShell SDK provides the following benefits:

  • Access to all Microsoft Graph APIs: Microsoft Graph PowerShell is based on Microsoft Graph API. In addition to Azure AD, the Microsoft Graph API includes APIs from other Microsoft services like SharePoint, Exchange, and Outlook, all accessed through a single endpoint with a single access token.
  • Supports PowerShell 7: Microsoft Graph PowerShell works with PowerShell 7 and later. It’s also compatible with Windows PowerShell 5.1.
  • Cross-platform support: Microsoft Graph PowerShell works on all platforms including Windows, macOS, and Linux.
  • Supports modern authentication: Microsoft Graph PowerShell supports the Microsoft Authentication Library (MSAL) which offers more security. For example, you can use passwordless sign-in experiences.
  • Supports external identities: Users from other Azure AD tenants can authenticate to services in your tenant with Microsoft Graph PowerShell.
  • Uses least privilege: Microsoft Graph PowerShell permissions are not pre-authorized and users must perform one-time request for app permissions depending on their needs.
  • Advanced queries: Microsoft Graph PowerShell supports rich, advanced queries via eventual consistency. For example, you can get a near-instant count of all users using advanced queries.
  • Open source: Feature teams and the community can create great PowerShell experiences and share them with everyone.
  • Receives regular updates: Microsoft Graph PowerShell commands are updated regularly to support the latest Graph API updates.

Microsoft Graph PowerShell module prerequisites

The following prerequisites are required to use the Microsoft Graph PowerShell SDK with Windows PowerShell:

  • Upgrade to PowerShell 5.1 or later
  • Install .NET Framework 4.7.2 or later

Install Microsoft Graph PowerShell module

To install Microsoft Graph PowerShell SDK module on the system, go through the below steps.

1. Set Windows PowerShell Execution Policy

By default, we can’t install scripts. To require all PowerShell scripts that you download from the internet are signed by a trusted publisher, run PowerShell as administrator, and run the cmdlet.

Set-ExecutionPolicy RemoteSigned -Force

Important: Close and re-open the elevated Windows PowerShell window to have the changes apply.

2. Install PowerShellGet module

Run PowerShell as administrator. Run the command Install-Module PowershellGet -Force. When asked to install NuGet provider, press Y and follow with Enter.

Install-Module PowershellGet -Force

If you get an error that it’s unable to install, read the article Unable to install NuGet provider for PowerShell.

3. Install Microsoft Graph module

Install the Microsoft Graph module.

Install-Module Microsoft.Graph -Force

It may take some time to download and install the Microsoft Graph PowerShell module. So give it a couple of minutes.

Note: We recommend also installing the Microsoft Graph Beta module in the next step. That’s because some cmdlets are not yet available in the final version, and they will not work.

4. Install Microsoft Graph Beta module

Install the Microsoft Graph Beta module.

Run Install-Module with -AllowClobber and -Force parameters to prevent conflicts when upgrading from other module versions.

Install-Module Microsoft.Graph.Beta -AllowClobber -Force

It may take some time to download and install the Microsoft Graph Beta PowerShell module. So give it a couple of minutes.

Connect to Microsoft Graph PowerShell

Connect to Microsoft Graph PowerShell using the module with or without MFA.

Note: We recommend to enable MFA (Multi-Factor Authentication) on the accounts.

Connect with your admin account to Microsoft Graph so you can access Azure Active Directory (Azure AD) resources. Run the Connect-MgGraph cmdlet.

Connect-MgGraph -Scopes "User.Read.All"

In the sign-in window that opens, enter your password, and then click Sign in.

If MFA is enabled, a verification code is generated and delivered based on the verification response option configured for your account. For example, a text message or the Microsoft Authenticator app on your mobile phone.

Install Microsoft Graph PowerShell module sign in

Enable the checkbox Consent on behalf of your organization. Click Accept.

Install Microsoft Graph PowerShell module permissions requested

After the verification succeeds, you will get back to the PowerShell window.

Read more: How to connect to Microsoft Graph PowerShell with different methods »

Get Microsoft Graph current session details

Download the Get-MgInfo.ps1 PowerShell script and save it in the C:\scripts folder.

Ensure the file is unblocked to prevent errors when running the script. Read more in the article Not digitally signed error when running PowerShell script.

Another option is to copy and paste the below code into Notepad. Give it the name Get-MgInfo.ps1 and place it in the C:\scripts folder.

Connect-MgGraph -Scopes "User.Read.All"

$Details = Get-MgContext
$Scopes = $Details | Select-Object -ExpandProperty Scopes
$Scopes = $Scopes -join ","
$OrgName = (Get-MgOrganization).DisplayName

"Microsoft Graph current session details:"
"---------------------------------------"
"Tenant Id = $($Details.TenantId)"
"Client Id = $($Details.ClientId)"
"Org name  = $OrgName"
"App Name  = $($Details.AppName)"
"Account   = $($Details.Account)"
"Scopes    = $Scopes"

Let’s retrieve the details about our current session and run the Get-MgInfo.ps1 PowerShell script from the C:\scripts folder.

C:\scripts\.\Get-MgInfo.ps1

The output appears.

Microsoft Graph current session details:
---------------------------------------
Tenant Id = 1d845768-c027-4321-abe5-02f619863465
Client Id = 16d83eec-204b-4c2f-b7e8-234a70dab27e
Org name  = exoip365
App Name  = Microsoft Graph Command Line Tools
Account   = admin@exoip.com
Scopes    = openid,profile,User.Read,email,User.Read.All

If you want to check that you have connected with Users Read All Permissions, an excellent way is to run Get-MgUser cmdlet and get a list of all the users.

Get-MgUser -All

If you want to use the Microsoft Graph Beta, the cmdlet is Get-MgBetaUser.

Get-MgBetaUser -All

Disconnect Microsoft Graph

Disconnect the remote PowerShell session when you’re finished. If you close the Microsoft Graph PowerShell module window without disconnecting the session, you could use up all the remote PowerShell sessions available to you, and you’ll need to wait for the sessions to expire.

Disconnect-MgGraph

Suppose you rerun the Disconnect-MgGraph cmdlet, you get the below output because you are already signed out.

Disconnect-MgGraph : No application to sign out from.
At line:1 char:1
+ Disconnect-MgGraph
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Disconnect-MgGraph], ArgumentException
    + FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Authentication.Cmdlets.DisconnectMgGraph

Check Microsoft Graph PowerShell module version

Verify that the Microsoft Graph module is installed and which version is running.

Get-Module -ListAvailable Microsoft.Graph | ft -AutoSize

Verify that the Microsoft Graph Beta module is installed and which version is running.

Get-Module -ListAvailable Microsoft.Graph.Beta | ft -AutoSize

Check Microsoft Graph PowerShell module latest available version

Before you run the Microsoft Graph PowerShell module, you want to check if there is an update available.

Find-Module Microsoft.Graph | ft -AutoSize

Update Microsoft Graph Powershell module

Microsoft provides updates to the Microsoft Graph PowerShell module, and you should keep it up to date.

Note: If the update does not work, remove the Microsoft Graph module and exit all PowerShell sessions. Next, start PowerShell and install Microsoft Graph.

Update Microsoft Graph PowerShell SDK module.

Update-Module Microsoft.Graph -Force

Update the Microsoft Graph Beta PowerShell SDK module.

Update-Module Microsoft.Graph.Beta -Force

Uninstall Microsoft Graph PowerShell module

To uninstall all the Microsoft Graph PowerShell modules, including the beta version, run the below script.

Note: The uninstallation can take time and depends on how many module versions are installed. Leave it running, and do not close the PowerShell session.

# Get all modules starting with "Microsoft.Graph" except for "Microsoft.Graph.Authentication" and select unique names
$Modules = Get-Module Microsoft.Graph* -ListAvailable | Where-Object { $_.Name -ne "Microsoft.Graph.Authentication" } | Select-Object Name -Unique

# For each module, get its name and list available versions
foreach ($Module in $Modules) {
    $ModuleName = $Module.Name
    $Versions = Get-Module $ModuleName -ListAvailable

    # For each version, uninstall the module with the specific version
    foreach ($Version in $Versions) {
        $ModuleVersion = $Version.Version
        Write-Host "Uninstall-Module $ModuleName $ModuleVersion" -ForegroundColor Cyan
        Uninstall-Module $ModuleName -RequiredVersion $ModuleVersion
    }
}

# Get Microsoft.Graph.Authentication module and list available versions
$ModuleName = "Microsoft.Graph.Authentication"
$Versions = Get-Module $ModuleName -ListAvailable

# For each version, uninstall the module with the specific version
foreach ($Version in $Versions) {
    $ModuleVersion = $Version.Version
    Write-Host "Uninstall-Module $ModuleName $ModuleVersion" -ForegroundColor Cyan
    Uninstall-Module $ModuleName -RequiredVersion $ModuleVersion
}

That’s it!

Keep reading: Enable idle session timeout in Microsoft 365 »

Conclusion

You learned how to install Microsoft Graph PowerShell. Connect to Azure AD using the Microsoft Graph PowerShell module with or without MFA. After connecting to Microsoft Graph PowerShell, you can use the cmdlets. Remember to disconnect when you finish.

Did you enjoy this article? You may also like Disable remote PowerShell for non-admins. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *