Skip to content

Install Microsoft Graph PowerShell module

Microsoft Graph PowerShell replaces the Azure AD PowerShell and MSOnline modules. Therefore, we suggest you to use MS Graph PowerShell SDK to connect with Azure AD. In this article, you will learn how to install Microsoft Graph PowerShell module.

Prerequisites Microsoft Graph PowerShell

Before you can install Microsoft Graph PowerShell module, you need to set up the system. 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

However, there are no additional prerequisites to use the Microsoft Graph PowerShell SDK if you install PowerShell 7 on Windows. Therefore it is recommended to use PowerShell 7 or later with Microsoft Graph PowerShell SDK on all platforms.

Install Microsoft Graph PowerShell SDK

The Microsoft Graph PowerShell SDK consist of two modules. Follow the below steps to install Microsoft Graph module and Microsoft Graph Beta module on PowerShell.

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, you need to follow the below steps.

Run PowerShell as administrator, and run the below cmdlet.

Set-ExecutionPolicy RemoteSigned -Force

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

2. Install PowerShellGet module

Run PowerShell as administrator and run the below cmdlet.

Install-Module PowershellGet -Force

3. Install Microsoft Graph module

Run the below command to install Microsoft Graph module. Press Y and Enter.

Install-Module Microsoft.Graph -Force

It may take a couple of minutes to download and install the Microsoft Graph module.

Note: We also suggest you install the Microsoft Graph Beta module in the next step. This is because some cmdlets will not work, as they are not available in the final version.

4. Install Microsoft Graph Beta module

Install the Microsoft Graph Beta module. Add the parameters -AllowClobber and -Force to prevent conflicts when upgrading from other module versions. This may be the case when upgrading to a new version.

Run the below command to install Microsoft Graph Beta module. Press Y and Enter.

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

It may take a couple of minutes to download and install the Microsoft Graph Beta module.

Check Microsoft Graph PowerShell module version

Verify you installed the Microsoft Graph module.

Use the below PowerShell cmdlet.

Get-InstalledModule Microsoft.Graph | ft -AutoSize

The output shows which MS Graph version is running.

Version Name            Repository Description
------- ----            ---------- -----------
2.16.0  Microsoft.Graph PSGallery  Microsoft Graph PowerShell module

Then also check you installed the Microsoft Graph Beta module.

Run the below Powershell cmdlet.

Get-InstalledModule Microsoft.Graph.Beta | ft -AutoSize

The PowerShell output shows which MS Graph Beta version is running.

Version Name                 Repository Description
------- ----                 ---------- -----------
2.16.0  Microsoft.Graph.Beta PSGallery  Microsoft Graph PowerShell module

Connect to Microsoft Graph PowerShell

You can connect to Microsoft Graph PowerShell with or without MFA. For this example we will show you how to connect with interactive mode.

Run the PowerShell cmdlet with the below scopes.

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

The sign-in window opens.

  • Enter you admin account and password
  • Click Sign in
Install Microsoft Graph PowerShell module

If you have MFA enabled, you will get a verification code sent as a text message or to your Authentication app on your phone. If your MFA is disabled, you will not do anything here.

Install Microsoft Graph PowerShell module

It shows the permissions you allow Microsoft Graph to use.

  • Select Consent on behalf of your organization
  • Click Accept
Install Microsoft Graph PowerShell module

After the verification, you can go back to your PowerShell window.

There are other methods to Connect to Microsoft Graph PowerShell without authentication, like self-signed certificate or client secret.

To verify you connected with Users Read All permissions, use Get-MgUser cmdlet.

Get-MgUser -All

Run the below cmdlet to use the Microsoft Graph Beta.

Get-MgBetaUser -All

Disconnect Microsoft Graph PowerShell

Always disconnect the remote PowerShell session when you finish, to avoid waiting for older sessions to expire.

Disconnect the remote PowerShell session once you finish with the below cmdlet.

Disconnect-MgGraph

You can check you disconnected correctly, if you run the above cmdlet again.

The below output shows there is no application to sign out from, because you already disconnected Microsoft Graph.

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.DisconnectMgGrap

Update Microsoft Graph PowerShell module

We recommend you always update to the latest version of the Microsoft Graph PowerShell module.

Run the below PowerShell command.

Update-Module Microsoft.Graph -Force
Update-Module Microsoft.Graph.Beta -Force

It will update the Microsoft Graph and Microsoft Graph Beta modules to the latest version available.

Uninstall Microsoft Graph PowerShell module

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

Important: You need to run all the commands below to completely remove Microsoft Graph from the system.

Uninstall-Module Microsoft.Graph -AllowPrerelease -AllVersions
Get-InstalledModule Microsoft.Graph.Beta* | Uninstall-Module -AllowPrerelease -AllVersions
Get-InstalledModule Microsoft.Graph.* | ? Name -ne "Microsoft.Graph.Authentication" | Uninstall-Module -AllowPrerelease -AllVersions
Uninstall-Module Microsoft.Graph.Authentication -AllowPrerelease -AllVersions

Note: It can take around 15 minutes for the uninstallation to complete. Wait, and don’t close the PowerShell session.

You can get the error: No match was found. The error looks like the example shown below, but it is not a problem. It means that the specified Microsoft Graph module is already removed. Proceed with the other commands.

PackageManagement\Uninstall-Package : No match was found for the specified search criteria and module names 'Microsoft.Graph'.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:12733 char:21
+ ...        $null = PackageManagement\Uninstall-Package @PSBoundParameters
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...ninstallPackage:UninstallPackage) [Uninstall-Package], Exception
    + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.UninstallPackage

That’s it!

Frequently Asked Questions (FAQ)

How to run Microsoft Graph PowerShell as administrator?

Click on Start and search for the PowerShell app. On the right side, click on Run as administrator. Or you can right-click the PowerShell icon and select Run as administrator. This way, you can run PowerShell commands without restrictions.

Why do I need to install Microsoft Graph PowerShell?

Installing Microsoft Graph PowerShell provides access to Azure AD and MSOnline modules. Since Azure Active Directory Graph is deprecated, it’s required to install Microsoft Graph PowerShell. Microsoft Graph is also more secure and resilient than Azure AD Graph.

Do I need multi-factor authentication (MFA) when I connect to Microsoft Graph PowerShell?

To connect to Microsoft Graph PowerShell, you don’t need MFA enabled. The cmdlet Connect-MgGraph works for accounts with or without MFA. However, we recommend enabling MFA to protect your organization and have strong security.

Read more: Connect to Microsoft Graph module »

Which Microsoft Graph PowerShell module do I need to install?

It’s best to install the necessary modules including Microsoft.Graph.Authentication which is installed by default. If you using an older MS Graph module, you should update it to the latest module using the cmdlet Update-Module Microsoft.Graph -Force and Update-Module Microsoft.Graph.Beta -Force.

Do I need to install the Microsoft Graph Beta module?

We recommend to install the Microsoft.Graph.Beta module to use commands that are not yet available in the Microsoft.Graph module. Otherwise, you can’t run these commands and you will get the error: No match was found.

Read more: Connect to Exchange Online PowerShell »

Conclusion

You learned how to install Microsoft Graph PowerShell module and update to the latest version. It is recommended to install the Microsoft Graph Beta module to use the cmdlets that are not yet available in the latest version v2. Connect to Microsoft Graph Powershell with or without MFA to use the new cmdlets. Always remember to disconnect when you finish.

Did you enjoy this article? You may also like Install Exchange Online PowerShell module. Don’t forget to follow us and share this article.

o365info Team

o365info Team

This article was written by our team of experienced IT architects, consultants, and engineers.

This Post Has 0 Comments

Leave a Reply

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