/commands/microsoft-graph/get-mgcontext
Get-MgContext: check the Microsoft Graph PowerShell connection, account, tenant and scopes
- tool
- Microsoft Graph PowerShell
- versions
- Microsoft.Graph.Authentication 2.41.2
- os
- windows, linux, macos
Get-MgContext shows who you are signed in as in Microsoft Graph PowerShell and what the token allows:
Get-MgContext
Get-MgContext | Select-Object -ExpandProperty ScopesThe most useful properties are Account (signed-in user), TenantId, ClientId (the app used to sign in), AuthType (Delegated or AppOnly), Scopes (permissions in the token) and ContextScope (CurrentUser or Process).
Not connected: no output
Without a connection Get-MgContext returns $null and writes no error, so it is safe to use as a test. Checked with Microsoft.Graph.Authentication 2.41.2:
Import-Module Microsoft.Graph.Authentication
$ctx = Get-MgContext
'Connected: ' + [bool]$ctx
'Output is null: ' + ($null -eq $ctx)Output
Connected: False Output is null: True
if (-not (Get-MgContext)) {
Connect-MgGraph -Scopes "User.Read.All" -NoWelcome
}Check for a scope before a call
$needed = 'User.ReadWrite.All'
if ((Get-MgContext).Scopes -notcontains $needed) {
Connect-MgGraph -Scopes $needed
}For app-only sessions Scopes lists the application permissions granted to the app.
Properties
| Property | Meaning |
|---|---|
| Account | User principal name of the signed-in user (empty for app-only) |
| TenantId | Tenant the token was issued for |
| ClientId | App ID; 14d82eec-204b-4c2f-b7e8-296a70dab67e is the Microsoft Graph Command Line Tools app |
| AuthType | Delegated or AppOnly |
| Scopes | Permissions in the token |
| ContextScope | CurrentUser (cached for your Windows or OS user) or Process (this session only) |
| CertificateThumbprint | Certificate used for app-only sign-in |
| Environment | Cloud, for example Global |
Get-MgContext reads the local session only; it does not call Graph and does not tell you whether the signed-in user holds an Entra role.
When to use it
Run Get-MgContext first whenever a Graph cmdlet fails. It answers the three questions behind most Microsoft Graph PowerShell errors without making a network call: am I connected at all, as which account and tenant, and with which permissions. In scripts it is the standard guard before work starts, because it returns $null instead of throwing when there is no session.
Troubleshooting with the context
| What you see | What it means | Next step |
|---|---|---|
| No output | Not connected in this session | Connect-MgGraph; cmdlets otherwise fail with "Authentication needed. Please call Connect-MgGraph." |
| Wrong Account or TenantId | A cached sign-in or a different tenant was used | Disconnect-MgGraph, then Connect-MgGraph -TenantId with the right tenant |
| Scope missing from Scopes | The token does not carry the permission the cmdlet needs | Reconnect with -Scopes and consent; Find-MgGraphCommand shows the permissions a cmdlet needs |
| AuthType AppOnly, empty Account | Signed in as an app with a certificate or secret | Permissions come from the app registration, not from -Scopes |
| ContextScope CurrentUser | The token cache is shared by later sessions of the same OS user | Use -ContextScope Process on shared machines |
All context properties
The properties of the context object in Microsoft.Graph.Authentication 2.41.2, read offline:
Import-Module Microsoft.Graph.Authentication
([Microsoft.Graph.PowerShell.Authentication.IAuthContext].GetProperties().Name | Sort-Object) -join ', 'Output
Account, AppName, AuthType, Certificate, CertificateSubjectName, CertificateThumbprint, ClientId, ClientSecret, ContextScope, Environment, HomeAccountId, LoginHint, ManagedIdentityId, PSHostVersion, Scopes, SendCertificateChain, TenantId, TokenCredentialType, WamEnabled
Besides the ones described above, AppName is the display name of the client app, TokenCredentialType shows how the token was obtained (InteractiveBrowser, DeviceCode, ClientCertificate, ClientSecret, ManagedIdentity, ...), and WamEnabled tells whether the Windows Web Account Manager broker was used. Related pages: Connect-MgGraph -Scopes, Authentication needed and Insufficient privileges.
FAQ
Get-MgContext shows the scope but the call still fails with insufficient privileges. Why?
In delegated sessions the token's scopes are only half of the check: the signed-in user also needs an Entra role or ownership that allows the action. Get-MgContext cannot show roles, so check the user's role assignments.
How do I switch to another account or tenant?
Run Disconnect-MgGraph, then Connect-MgGraph again with -TenantId and the scopes you need. Get-MgContext should then show the new Account and TenantId.
Does Get-MgContext need any Graph permission?
No. It reads the session held by the module in memory and never calls Microsoft Graph, so it works with any connection and returns nothing without one.