Consume Microsoft Graph resources directly from your PowerShell scripts
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.
Modules | Getting Started | API Version | Notes | Troubleshooting | Known Issues | Feedback | License
Important
Starting February 23, 2027 at 11:00 a.m. Pacific Standard Time (7:00 p.m. Coordinated Universal Time), Microsoft plans to require a more secure browser response for interactive sign-in through the default Microsoft Graph PowerShell application.
You must upgrade to Microsoft Graph PowerShell version 2.37.0 or later before enforcement if all the following conditions apply:
- You use Microsoft Graph PowerShell version 2.36.0 or earlier.
- You use browser-based interactive sign-in.
- You use the default Microsoft Graph PowerShell application.
Otherwise, sign-in or reauthentication will fail and Microsoft Graph PowerShell won't obtain an access token. Windows brokered sign-in, device code authentication, application-only authentication, customer-owned application registrations, and access tokens supplied by the caller aren't directly affected.
For update instructions, see Updating the SDK.
This repository publishes complete bundle modules, the authentication module, and scenario-specific modules for the Microsoft Graph v1.0 and beta endpoints. For faster installation and import, prefer the scenario-specific modules you need instead of the complete Microsoft.Graph and Microsoft.Graph.Beta bundle modules. The bundle modules install all their respective scenario-specific modules as dependencies.
PowerShell 7 or later is recommended on all platforms. Install the modules you need from PowerShell Gallery. For the examples below:
Install-Module Microsoft.Graph.Users -Scope CurrentUser
Install-Module Microsoft.Graph.Applications -Scope CurrentUserThese modules automatically install Microsoft.Graph.Authentication, which provides Connect-MgGraph. To install all v1.0 modules instead, use Install-Module Microsoft.Graph -Scope CurrentUser.
If you encounter command name conflicts when upgrading from v1.x to v2.x, use
-AllowClobberand-Force:Install-Module Microsoft.Graph -AllowClobber -Force
See Install the Microsoft Graph PowerShell Module guide for detailed installation instructions.
The module supports two main types of authentication:
Get access to Microsoft Graph resources on behalf of a user. Request only the delegated permissions needed for your operations; some permissions require administrator consent.
# Using interactive authentication.
Connect-MgGraph -Scopes "User.ReadBasic.All", "Application.ReadWrite.All"On Windows, interactive sign-in uses Web Account Manager (WAM) by default. -LoginHint "user@contoso.com" can pre-populate the account for interactive sign-in; it does not guarantee a prompt-free sign-in. For device code authentication, use -UseDeviceCode.
Delegated sign-in uses the CurrentUser token cache by default, so it can persist across PowerShell sessions. Use -ContextScope Process to keep tokens in the current process instead; this does not select a different account. Inspect the connected account, tenant, and permissions with Get-MgContext.
Get access to Microsoft Graph resources using an application's identity, not on behalf of a user. Configure the required application permissions and grant administrator consent before connecting.
# Using -CertificateThumbprint
Connect-MgGraph -ClientId "YOUR_APP_ID" -TenantId "YOUR_TENANT_ID" -CertificateThumbprint "YOUR_CERT_THUMBPRINT"See Authentication for client secret, managed identity, custom application, sovereign cloud, and WAM configuration examples.
Get-MgUser -Top 10 -Property Id, DisplayName, BusinessPhones | Format-Table Id, DisplayName, BusinessPhones$User = Get-MgUser -Filter "displayName eq 'Megan Bowen'"New-MgApplication -DisplayName "ScriptedGraphPSApp" `
-SignInAudience "AzureADMyOrg" `
-Web @{ RedirectUris = "https://localhost"}Disconnect-MgGraphThis clears the SDK's in-memory authentication state and, for CurrentUser, clears persisted tokens for the session's account when it can be identified. It does not clear the Windows WAM broker cache by default. See Sign out and token caching before using -SignOutFromBroker.
Use v1.0 APIs for production scripts. Beta APIs are preview features and can introduce breaking changes. Install the beta scenario-specific modules you need (or Microsoft.Graph.Beta for all beta modules); v1.0 and beta modules can coexist and share authentication.
Install-Module Microsoft.Graph.Beta.Users -Scope CurrentUser
# Consume Microsoft Graph beta resources.
Connect-MgGraph -Scopes "User.ReadBasic.All"
$Users = Get-MgBetaUserThe Microsoft Graph PowerShell SDK is scheduled to be updated during the fourth week of each month
The following breaking changes have been introduced between v1.x and v2.x:
- Dropped profile support.
- Dropped support for
-ForceRefreshonConnect-MgGraph. - Renamed
betacommand names from<Verb>-Mg<Noun>to<Verb>-MgBeta<Noun>. - Renamed
DeviceManagement.Enrolmentmodule toDeviceManagement.Enrollment. - Moved directory role and entitlement management commands from
DeviceManagement.EnrollmenttoIdentity.Governancemodule. - Changed beta namespace from
Microsoft.Graph.PowerShell.Models.<Entity>toMicrosoft.Graph.Beta.PowerShell.Models.<Entity>. - Changed
-AccessTokentype onConnect-MgGraphfromStringtoSecureString.
See the v2 upgrade guide for more details.
When working with various operations in the Graph, you may encounter an error such as "Insufficient privileges to complete the operation." For example, this particular error can occur when using the New-MgApplication command if the appropriate permissions are not granted.
Check the tenant, authentication type, and granted permissions with Get-MgContext. Delegated access also depends on the signed-in user's privileges; app-only access requires application permissions with administrator consent. After permissions or consent change, allow time for propagation, then run Disconnect-MgGraph and reconnect with the required scopes or app credentials. Caller-provided access tokens must be replaced by the caller.
See Microsoft Graph API Permissions Reference for more details.
See our troubleshooting guide for a detailed view of how to troubleshoot common errors when using Microsoft Graph.
-
Using the
-Property {PropertyName}parameter will not select the property as the output of the command. All commands return CLR objects, and customers should pipe the command outputs toFormat-TableorSelect-Objectto return individual properties. -
Customers upgrading from previous versions of the SDK may encounter authentication prompts on every command call. Start by running
Disconnect-MgGraph, then reconnect withConnect-MgGraph. Do not delete the entire.mgdirectory: it also contains SDK options and environment settings. For Windows broker sign-in issues, see WAM guidance, including the custom-ClientIdrequirement when disabling WAM.
If you find any bugs when using the Microsoft Graph PowerShell modules, please file an issue on our GitHub issues page.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT license.
