Microsoft Graph PowerShell commands cheat sheet
The Microsoft Graph PowerShell SDK replaces the retired AzureAD and MSOnline modules. Each cmdlet needs a Graph permission scope, which you request with Connect-MgGraph -Scopes; least-privileged scopes from Microsoft Learn are listed in the notes.
- Covers
- Microsoft.Graph PowerShell SDK 2.x (v1.0 endpoint)
- Commands
- 45
- Last verified
Install and connect
| Command | What it does |
|---|---|
Install-Module Microsoft.Graph -Scope CurrentUser | Install the Microsoft Graph PowerShell SDK (v1.0 cmdlets) for the current user. Example Install-Module Microsoft.Graph -Scope CurrentUser -Repository PSGallery -ForceBeta cmdlets live in a separate module: Install-Module Microsoft.Graph.Beta. Select-MgProfile no longer exists in v2. |
Update-Module Microsoft.Graph | Update the SDK and its sub-modules to the latest release. |
Connect-MgGraph -Scopes <scopes> | Sign in with delegated access and request the permission scopes you need. Example Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All"Add -UseDeviceAuthentication on a machine without a browser. Run it again with extra scopes to add permissions. |
Connect-MgGraph -ClientId <app-id> -TenantId <tenant-id> -CertificateThumbprint <thumbprint> | Sign in app-only with an app registration and a certificate, for unattended scripts. Example Connect-MgGraph -ClientId "<app-id>" -TenantId "contoso.onmicrosoft.com" -CertificateThumbprint "<thumbprint>" |
Get-MgContext | Show the signed-in account, tenant, auth type and granted scopes. Example Get-MgContext | Select-Object -ExpandProperty Scopes |
Find-MgGraphCommand -Command <cmdlet> | Show the API URI a cmdlet calls and the permissions it can use. Example Find-MgGraphCommand -Command Get-MgUserUse -Uri "/users/{id}" to go the other way and find cmdlets for an API path. |
Find-MgGraphPermission <keyword> | Search Graph permission names and descriptions. Example Find-MgGraphPermission application.Read | Format-List |
Disconnect-MgGraph | Sign out and end the current Graph session. |
Users
| Command | What it does |
|---|---|
Get-MgUser -UserId <id-or-upn> | Get one user by object ID or user principal name. Example Get-MgUser -UserId [email protected] | Format-List Id,DisplayName,Mail,UserPrincipalNameScope: User.Read.All (User.ReadBasic.All for basic properties only). |
Get-MgUser -All -Property <list> | List every user, returning the properties you ask for. Example Get-MgUser -All -Property Id,DisplayName,UserPrincipalName,AccountEnabled,UsageLocation | Select-Object DisplayName,UserPrincipalName,AccountEnabled,UsageLocationWithout -All only the first page is returned. Many properties, such as AccountEnabled, are only filled in when you name them in -Property. |
Get-MgUser -Filter <odata-filter> | Find users with an OData filter. Example Get-MgUser -Filter "startsWith(displayName,'a')" -Top 10 |
Get-MgUser -ConsistencyLevel eventual -CountVariable <name> -Filter <odata-filter> | Run an advanced query (endsWith, ne, not, counts) that needs the eventual consistency header. Example Get-MgUser -ConsistencyLevel eventual -CountVariable userCount -Filter "endsWith(mail,'@contoso.com')" -AllAdvanced queries need both -ConsistencyLevel eventual and -CountVariable. The count lands in $userCount. |
Get-MgUser -ConsistencyLevel eventual -CountVariable <name> -Filter 'assignedLicenses/$count eq 0' | List users who have no licence assigned. Example Get-MgUser -ConsistencyLevel eventual -CountVariable unlicensed -Filter 'assignedLicenses/$count eq 0' -AllUse single quotes so PowerShell does not expand $count. |
New-MgUser -DisplayName <name> -PasswordProfile <hashtable> -AccountEnabled -MailNickname <alias> -UserPrincipalName <upn> | Create a cloud user account. Example $PasswordProfile = @{ Password = '<temporary-password>'; ForceChangePasswordNextSignIn = $true }; New-MgUser -DisplayName 'Alex Wilber' -PasswordProfile $PasswordProfile -AccountEnabled -MailNickname 'alexw' -UserPrincipalName '[email protected]'Scope: User.ReadWrite.All (least privileged: User.Create). |
Update-MgUser -UserId <id-or-upn> -UsageLocation <country-code> | Set the user's usage location, which must be set before a licence can be assigned. Example Update-MgUser -UserId [email protected] -UsageLocation GBTwo-letter ISO 3166 code such as US, GB or IN. Scope: User.ReadWrite.All (least privileged: User.ReadUpdate.All). |
Update-MgUser -UserId <id-or-upn> -AccountEnabled:$false | Block a user from signing in by disabling the account. Example Update-MgUser -UserId [email protected] -AccountEnabled:$falseWarning: Warning: the user is blocked from signing in to every app. Least-privileged scopes: User.EnableDisableAccount.All plus User.Read.All; disabling an admin needs a suitably privileged admin role. Use -AccountEnabled to re-enable. |
Revoke-MgUserSignInSession -UserId <id-or-upn> | Invalidate a user's refresh tokens and browser session cookies so they must sign in again. Example Revoke-MgUserSignInSession -UserId [email protected]Warning: Warning: signs the user out of every app and device; there can be a delay of a few minutes. Scope: User.RevokeSessions.All. |
Remove-MgUser -UserId <id-or-upn> | Delete a user account. Example Remove-MgUser -UserId [email protected] -ConfirmWarning: Warning: the user, mailbox and licence assignments move to deleted items and are permanently deleted after 30 days if not restored. Scope: User.ReadWrite.All. |
Restore-MgDirectoryDeletedItem -DirectoryObjectId <id> | Restore a recently deleted user, group, application or service principal from deleted items. Example Restore-MgDirectoryDeletedItem -DirectoryObjectId '<object-id>'Deleted items stay available for up to 30 days, then are deleted permanently. |
Get-MgUserMemberOf -UserId <id-or-upn> -All | List the groups and directory roles a user belongs to directly. Example Get-MgUserMemberOf -UserId [email protected] -All |
Get-MgUserManager -UserId <id-or-upn> | Get a user's manager. Example Get-MgUserManager -UserId [email protected] |
Licences
| Command | What it does |
|---|---|
Get-MgSubscribedSku -All | List the tenant's licence SKUs with SkuId, SkuPartNumber and consumed units. Example Get-MgSubscribedSku -All | Select-Object SkuPartNumber,SkuId,ConsumedUnitsScope: LicenseAssignment.Read.All (Organization.Read.All also works). |
Set-MgUserLicense -UserId <id-or-upn> -AddLicenses @{SkuId = <sku-id>} -RemoveLicenses @() | Assign a licence to a user. Example $Sku = Get-MgSubscribedSku -All | Where-Object SkuPartNumber -eq 'SPE_E3'; Set-MgUserLicense -UserId [email protected] -AddLicenses @{SkuId = $Sku.SkuId} -RemoveLicenses @()Both -AddLicenses and -RemoveLicenses are required, even when one is empty. The user needs a UsageLocation first. Scope: LicenseAssignment.ReadWrite.All. |
Set-MgUserLicense -UserId <id-or-upn> -AddLicenses @() -RemoveLicenses @(<sku-id>) | Remove a licence from a user. Example Set-MgUserLicense -UserId [email protected] -AddLicenses @() -RemoveLicenses @($Sku.SkuId)Warning: Warning: the user loses the services in that licence, and service data tied to it can be lost. Scope: LicenseAssignment.ReadWrite.All. |
Get-MgUserLicenseDetail -UserId <id-or-upn> | Show the licences and service plans assigned to a user. Example Get-MgUserLicenseDetail -UserId [email protected] | Select-Object SkuPartNumber,SkuIdScope: LicenseAssignment.Read.All. |
Groups
| Command | What it does |
|---|---|
Get-MgGroup -Filter <odata-filter> | Find groups, for example by display name. Example Get-MgGroup -Filter "displayName eq 'Sales Team'" | Format-List Id,DisplayName,GroupTypesScope: Group.Read.All. Use -All to list every group. |
New-MgGroup -DisplayName <name> -MailEnabled:$false -MailNickname <alias> -SecurityEnabled | Create a security group. Example New-MgGroup -DisplayName 'VPN Users' -MailEnabled:$false -MailNickname 'vpnusers' -SecurityEnabledScope: Group.ReadWrite.All. |
New-MgGroup -DisplayName <name> -GroupTypes 'Unified' -MailEnabled -MailNickname <alias> -SecurityEnabled:$false | Create a Microsoft 365 group. Example New-MgGroup -DisplayName 'Marketing' -GroupTypes 'Unified' -MailEnabled -MailNickname 'marketing' -SecurityEnabled:$false |
New-MgGroupMember -GroupId <group-id> -DirectoryObjectId <object-id> | Add a user or other directory object to a group. Example New-MgGroupMember -GroupId '<group-id>' -DirectoryObjectId (Get-MgUser -UserId [email protected]).IdScope: GroupMember.ReadWrite.All. |
Get-MgGroupMember -GroupId <group-id> -All | List every member of a group. Example Get-MgGroupMember -GroupId '<group-id>' -AllScope: GroupMember.Read.All. Results are directory objects; details such as the UPN are in AdditionalProperties. |
New-MgGroupOwner -GroupId <group-id> -DirectoryObjectId <object-id> | Add an owner to a group. Example New-MgGroupOwner -GroupId '<group-id>' -DirectoryObjectId '<user-object-id>' |
Remove-MgGroupMemberByRef -GroupId <group-id> -DirectoryObjectId <object-id> | Remove a member from a group. Example Remove-MgGroupMemberByRef -GroupId '<group-id>' -DirectoryObjectId '<user-object-id>'Warning: Warning: the member loses any access, licences or policies applied through the group. Scope: GroupMember.ReadWrite.All. |
Admin roles and authentication methods
| Command | What it does |
|---|---|
Get-MgDirectoryRole | List the directory roles that are activated in the tenant. Example Get-MgDirectoryRole | Select-Object Id,DisplayNameScope: RoleManagement.Read.Directory. Only activated roles are returned; Get-MgRoleManagementDirectoryRoleAssignment covers every assignment. |
Get-MgDirectoryRoleMember -DirectoryRoleId <role-id> | List members of an activated directory role, such as Global Administrator. Example Get-MgDirectoryRoleMember -DirectoryRoleId (Get-MgDirectoryRole -Filter "displayName eq 'Global Administrator'").IdScope: RoleManagement.Read.Directory. |
Get-MgRoleManagementDirectoryRoleAssignment -Filter <odata-filter> | List role assignments through the unified RBAC API, for example all roles held by one user. Example Get-MgRoleManagementDirectoryRoleAssignment -Filter "principalId eq '<user-object-id>'" -ExpandProperty roleDefinitionScope: RoleManagement.Read.Directory. principalId and roleDefinitionId support eq and in filters. |
Get-MgUserAuthenticationMethod -UserId <id-or-upn> | List a user's registered authentication methods, such as Authenticator, phone and FIDO2. Example Get-MgUserAuthenticationMethod -UserId [email protected]Scope for other users: UserAuthenticationMethod.Read.All; the admin also needs a role such as Global Reader or Authentication Administrator. |
Sign-in and audit logs
| Command | What it does |
|---|---|
Get-MgAuditLogSignIn -Filter <odata-filter> -Top <n> | Read Entra ID sign-in logs. Example Get-MgAuditLogSignIn -Filter "userPrincipalName eq '[email protected]'" -Top 20Scope: AuditLog.Read.All. userPrincipalName supports eq and startsWith; createdDateTime supports eq, le and ge. Only data within the Entra retention period is returned. |
Get-MgAuditLogSignIn -Filter "createdDateTime ge <date>" | Read sign-ins since a given UTC date and time. Example Get-MgAuditLogSignIn -Filter "createdDateTime ge 2026-10-27T00:00:00Z" -Top 100 |
Get-MgAuditLogDirectoryAudit -Filter <odata-filter> | Read the Entra ID audit log of directory changes. Example Get-MgAuditLogDirectoryAudit -Filter "initiatedBy/user/id eq '<user-object-id>'" -Top 50Scope: AuditLog.Read.All. |
Devices and applications
| Command | What it does |
|---|---|
Get-MgDevice -All | List devices registered or joined to Entra ID. Example Get-MgDevice -Filter "operatingSystem eq 'Windows'" -All | Select-Object DisplayName,OperatingSystemVersion,ApproximateLastSignInDateTimeScope: Device.Read.All. |
Get-MgUserRegisteredDevice -UserId <id-or-upn> | List devices registered by a user. Example Get-MgUserRegisteredDevice -UserId [email protected] |
Get-MgApplication -Filter <odata-filter> | List app registrations, or find one by its application (client) ID. Example Get-MgApplication -Filter "appId eq '<app-id>'" | Format-List Id,DisplayName,AppId,SignInAudienceScope: Application.Read.All. |
Get-MgServicePrincipal -Filter <odata-filter> | List enterprise applications (service principals) in the tenant. Example Get-MgServicePrincipal -Filter "displayName eq 'Power BI Service'" | Format-List Id,DisplayName,AppIdScope: Application.Read.All. |
Raw Graph requests
| Command | What it does |
|---|---|
Invoke-MgGraphRequest -Method GET -Uri <url> | Call any Microsoft Graph endpoint directly using the current session. Example Invoke-MgGraphRequest -Method GET -Uri 'https://graph.microsoft.com/v1.0/users?$top=5'Useful when no cmdlet exists yet. Use single quotes so PowerShell does not expand $top. |
Invoke-MgGraphRequest -Method PATCH -Uri <url> -Body <hashtable> | Send a write request, such as updating a user property, with a request body. Example Invoke-MgGraphRequest -Method PATCH -Uri 'https://graph.microsoft.com/v1.0/users/[email protected]' -Body @{ jobTitle = 'Sales Manager' } |
Official references
Last verified against these pages. Commands marked Warning restart, erase or remove something: run them only in a maintenance window and with a backup.
- Install the Microsoft Graph PowerShell SDK learn.microsoft.com
- Get started with the Microsoft Graph PowerShell SDK learn.microsoft.com
- Get-MgUser (Microsoft.Graph.Users) learn.microsoft.com
- Set-MgUserLicense (Microsoft.Graph.Users.Actions) learn.microsoft.com
- New-MgGroupMember (Microsoft.Graph.Groups) learn.microsoft.com
- Get-MgAuditLogSignIn (Microsoft.Graph.Reports) learn.microsoft.com
- Update user (Microsoft Graph API permissions) learn.microsoft.com
- Advanced query capabilities on Microsoft Entra ID objects learn.microsoft.com
Questions people ask
What replaces the AzureAD and MSOnline PowerShell modules?
The Microsoft Graph PowerShell SDK. The AzureAD and MSOnline modules are retired, so scripts using Get-AzureADUser or Get-MsolUser should move to Graph cmdlets such as Get-MgUser, Set-MgUserLicense and Get-MgGroupMember. Install it with Install-Module Microsoft.Graph -Scope CurrentUser.
Why does Get-MgUser return empty values for some properties?
Graph returns only a default set of user properties. Properties such as AccountEnabled, UsageLocation or SignInActivity must be requested with -Property, for example Get-MgUser -All -Property DisplayName,AccountEnabled. Use -All as well, or you only get the first page.
How do I assign a Microsoft 365 licence with Graph PowerShell?
Set the user's usage location with Update-MgUser -UsageLocation, find the SkuId with Get-MgSubscribedSku -All, then run Set-MgUserLicense -UserId [email protected] -AddLicenses @{SkuId = '<sku-id>'} -RemoveLicenses @(). Connect with the LicenseAssignment.ReadWrite.All scope.
What happened to Select-MgProfile?
It was removed in version 2 of the SDK. The Microsoft.Graph module now targets only the v1.0 endpoint, and beta cmdlets come from a separate Microsoft.Graph.Beta module with an MgBeta prefix, such as Get-MgBetaUser.