- Interactive Admin Work: Use
Connect-MgGraph -Scopes "User.Read.All"(Delegated) or-UseDeviceAuthenticationon headless jump boxes without a local browser. - Unattended Windows / On-Prem Scripts: Use Certificate-Based App-Only Auth (
-ClientId -TenantId -CertificateThumbprint) instead of static client secrets. - Cloud & CI/CD Pipelines: Use Managed Identity (
Connect-MgGraph -Identity) in Azure Automation/Functions or Federated Identity Credentials (OIDC) in GitHub Actions—zero secrets or certs to rotate. - Zero-SDK / Client Secret Pattern: Acquire an OAuth 2.0 bearer token via
Invoke-RestMethodand pass it toConnect-MgGraph -AccessToken(or call Graph REST endpoints directly).
Whether you are running a quick one-liner from your workstation, scheduling a nightly Entra ID audit on a Windows server, or automating user provisioning from Azure Automation or GitHub Actions, choosing the right Microsoft Graph PowerShell authentication method is critical for both reliability and security.
In this 2026 Master Reference, we demonstrate every supported way to connect to Microsoft Graph—from Interactive Delegated and Device Code to Client Secret, Certificate Thumbprint, Managed Identity, Federated OIDC, and Raw AccessToken—with a side-by-side decision matrix and live-tested output from our LAB 365 Dev Tenant.
Decision Matrix: Which Graph Auth Method Should You Use?
| Method | Auth Type | Secret / Cert Needed? | Best Use Case | Security Rating |
|---|---|---|---|---|
| 1. Interactive Browser | Delegated | No (User + MFA/Passkey) | Ad-hoc admin tasks on workstation | High |
| 2. Device Code Flow | Delegated | No (User + MFA/Passkey) | SSH, WSL, or headless jump boxes | High |
3. Client Secret (OAuth2) | App-Only | Yes (Static Secret) | Legacy/dev testing only (avoid in prod) | Low–Medium |
| 4. Certificate Thumbprint | App-Only | Yes (X.509 Cert) | On-prem Windows servers & scheduled tasks | High |
| 5. Managed Identity | App-Only | None (Azure-managed) | Azure Automation, Functions, VMs | Highest |
| 6. Federated OIDC | App-Only | None (OIDC Token) | GitHub Actions / Azure DevOps pipelines | Highest |
Architecture: How Connect-MgGraph Resolves Tokens
Method 1: Interactive Delegated Sign-In (With MFA & Passkeys)
Use this for day-to-day administrative work on your workstation. Connect-MgGraph opens your default browser, enforces Conditional Access / MFA, and requests only the least-privilege scopes you specify:
# Connect interactively with least-privilege delegated scopes
Connect-MgGraph -Scopes "User.Read.All", "Group.Read.All" -NoWelcome
# Verify your active session and granted scopes
Get-MgContext | Select-Object Account, AuthType, TenantId, Scopes
Method 2: Device Code Flow (Headless Terminals & Jump Boxes)
When working over SSH, Windows Server Core, or a remote session without a browser, use -UseDeviceAuthentication (or -UseDeviceCode in older v1/v2 builds) to display a short code you can approve on any phone or laptop:
Connect-MgGraph -Scopes "User.Read.All", "AuditLog.Read.All" -UseDeviceAuthentication
# Output: To sign in, use a web browser to open https://microsoft.com/devicelogin and enter the code XXXXXXXX
Method 3: Client Secret (client_credentials + -AccessToken)
Notice that Connect-MgGraph does not have a -ClientSecret parameter by design—Microsoft discourages static secrets in scripts. However, if you are testing in a dev/lab environment or migrating an existing secret-based script, you can request a bearer token from the Entra v2.0 token endpoint and pass it via -AccessToken:
$TenantId = "ab85c7a1-366d-44ba-acc6-924fa0950045"
$ClientId = "YOUR-APP-CLIENT-ID"
$ClientSecret = $env:GRAPH_CLIENT_SECRET # Never hardcode secrets in .ps1 files!
$TokenBody = @{
client_id = $ClientId
client_secret = $ClientSecret
scope = "https://graph.microsoft.com/.default"
grant_type = "client_credentials"
}
$TokenResponse = Invoke-RestMethod `
-Method Post `
-Uri "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token" `
-Body $TokenBody `
-ContentType "application/x-www-form-urlencoded"
# Pass the bearer token into Connect-MgGraph as a SecureString
$SecureToken = ConvertTo-SecureString $TokenResponse.access_token -AsPlainText -Force
Connect-MgGraph -AccessToken $SecureToken -NoWelcome
Get-MgContext | Select-Object AuthType, TokenCredentialType, ContextScope
Security Tip: If you use -AccessToken, the token expires after ~60–90 minutes and will not auto-refresh. For long-running jobs, use Certificate Auth (Method 4) or Managed Identity (Method 5) instead.
Method 4: Certificate-Based App-Only Auth (Recommended for On-Prem)
For scheduled tasks on Windows servers or local automation workstations, upload a .cer public key to your Entra App Registration and connect unattended using the private key in Cert:\CurrentUser\My\ (or Cert:\LocalMachine\My\):
Connect-MgGraph `
-ClientId "c841ae33-4093-4385-8a90-6a530761f9b9" `
-TenantId "ab85c7a1-366d-44ba-acc6-924fa0950045" `
-CertificateThumbprint "BEAC429DE404B46FF030811C0C9DE00DE701C027" `
-NoWelcome
Get-MgContext | Select-Object ClientId, TenantId, AuthType, TokenCredentialType, CertificateThumbprint, Scopes | Format-List
Live Output from Our LAB 365 (xzf58.onmicrosoft.com) Tenant:
ClientId : c841ae33-4093-4385-8a90-6a530761f9b9
TenantId : ab85c7a1-366d-44ba-acc6-924fa0950045
AuthType : AppOnly
TokenCredentialType : ClientCertificate
CertificateThumbprint : BEAC429DE404B46FF030811C0C9DE00DE701C027
Scopes : {User.ReadWrite.All, UserAuthenticationMethod.ReadWrite.All, Directory.ReadWrite.All, Group.ReadWrite.All...}
Method 5: Managed Identity (-Identity) for Azure Automation & Functions
If your PowerShell script runs inside Azure Automation, Azure Functions, or an Azure VM, enable a System-Assigned (or User-Assigned) Managed Identity, grant it Microsoft Graph application permissions via New-MgServicePrincipalAppRoleAssignment, and connect with zero credentials:
# System-Assigned Managed Identity
Connect-MgGraph -Identity -NoWelcome
# Or User-Assigned Managed Identity (specify ClientId)
# Connect-MgGraph -Identity -ClientId "00000000-0000-0000-0000-000000000000" -NoWelcome
Method 6: Federated Identity Credentials (GitHub Actions / CI-CD)
In GitHub Actions or Azure DevOps, configure a Federated Identity Credential on your Entra App Registration so the pipeline exchanges its short-lived OIDC token for a Microsoft Graph token—eliminating stored secrets and .pfx certificates completely:
# Inside GitHub Actions (after azure/login or OIDC token request)
$OidcToken = (Invoke-RestMethod -Uri "$($env:ACTIONS_ID_TOKEN_REQUEST_URL)&audience=api://AzureADTokenExchange" `
-Headers @{ Authorization = "Bearer $($env:ACTIONS_ID_TOKEN_REQUEST_TOKEN)" }).value
$TokenResp = Invoke-RestMethod -Method Post `
-Uri "https://login.microsoftonline.com/$($env:TENANT_ID)/oauth2/v2.0/token" `
-Body @{
client_id = $env:CLIENT_ID
client_assertion_type = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
client_assertion = $OidcToken
scope = "https://graph.microsoft.com/.default"
grant_type = "client_credentials"
}
Connect-MgGraph -AccessToken (ConvertTo-SecureString $TokenResp.access_token -AsPlainText -Force) -NoWelcome
Method 7: Raw Invoke-RestMethod With JWT Client Assertion (Zero SDK)
If you don't want to install the Microsoft.Graph PowerShell module on a lightweight runner, you can sign an RFC 7523 JWT client assertion directly using your local certificate and call https://graph.microsoft.com/v1.0/ with Invoke-RestMethod (this is the exact pattern used in our LAB 365 automated tenant smoke tests):
$Resp = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token" -Body @{
client_id = $ClientId
client_assertion_type = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
client_assertion = $SignedJwtAssertion
scope = "https://graph.microsoft.com/.default"
grant_type = "client_credentials"
} -ContentType "application/x-www-form-urlencoded"
$Headers = @{ Authorization = "Bearer $($Resp.access_token)" }
(Invoke-RestMethod -Method Get -Uri "https://graph.microsoft.com/v1.0/organization" -Headers $Headers).value |
Select-Object displayName, id
Live AccessToken Context Verified on LAB 365:
AuthType : UserProvidedAccessToken
TokenCredentialType : UserProvidedAccessToken
ContextScope : Process
References & Official Microsoft Documentation
- Microsoft Learn — Choose a Microsoft Graph PowerShell Authentication Strategy
- Microsoft Learn — Use App-Only Authentication with the Microsoft Graph PowerShell SDK
- Microsoft Learn — Connect-MgGraph (Microsoft.Graph.Authentication)
- Microsoft Learn — Workload Identity Federation in Microsoft Entra ID
đź’¬ Reader Comments & Feedback
Did this runbook help, or did Microsoft change a cmdlet or UI menu? Share your feedback below.
Leave a Comment or Report an Update