Skip to content

/commands/microsoft-graph/get-mgcontext

Get-MgContext: check the Microsoft Graph PowerShell connection, account, tenant and scopes

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
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:

powershell
Get-MgContext
Get-MgContext | Select-Object -ExpandProperty Scopes

The 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:

powershell
Import-Module Microsoft.Graph.Authentication
$ctx = Get-MgContext
'Connected: ' + [bool]$ctx
'Output is null: ' + ($null -eq $ctx)

Output

Connected: False
Output is null: True
powershell
if (-not (Get-MgContext)) {
  Connect-MgGraph -Scopes "User.Read.All" -NoWelcome
}

Check for a scope before a call

powershell
$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

PropertyMeaning
AccountUser principal name of the signed-in user (empty for app-only)
TenantIdTenant the token was issued for
ClientIdApp ID; 14d82eec-204b-4c2f-b7e8-296a70dab67e is the Microsoft Graph Command Line Tools app
AuthTypeDelegated or AppOnly
ScopesPermissions in the token
ContextScopeCurrentUser (cached for your Windows or OS user) or Process (this session only)
CertificateThumbprintCertificate used for app-only sign-in
EnvironmentCloud, 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 seeWhat it meansNext step
No outputNot connected in this sessionConnect-MgGraph; cmdlets otherwise fail with "Authentication needed. Please call Connect-MgGraph."
Wrong Account or TenantIdA cached sign-in or a different tenant was usedDisconnect-MgGraph, then Connect-MgGraph -TenantId with the right tenant
Scope missing from ScopesThe token does not carry the permission the cmdlet needsReconnect with -Scopes and consent; Find-MgGraphCommand shows the permissions a cmdlet needs
AuthType AppOnly, empty AccountSigned in as an app with a certificate or secretPermissions come from the app registration, not from -Scopes
ContextScope CurrentUserThe token cache is shared by later sessions of the same OS userUse -ContextScope Process on shared machines

All context properties

The properties of the context object in Microsoft.Graph.Authentication 2.41.2, read offline:

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

Sources