Skip to content

Instantly share code, notes, and snippets.

@jorgeasaurus
Last active September 29, 2025 14:45
Show Gist options
  • Select an option

  • Save jorgeasaurus/feab0f8d555e318dd08f8121594429f4 to your computer and use it in GitHub Desktop.

Select an option

Save jorgeasaurus/feab0f8d555e318dd08f8121594429f4 to your computer and use it in GitHub Desktop.
This PowerShell code retrieves the current user's Microsoft Graph API access token by making a GET request to the /me endpoint and extracting the Bearer token from the Authorization header of the request.
function Get-MgToken {
$Parameters = @{
Method = "GET"
URI = "/v1.0/users?$top=1"
OutputType = "HttpResponseMessage"
}
$Response = Invoke-MgGraphRequest @Parameters
$Headers = $Response.RequestMessage.Headers
$Token = $Headers.Authorization.Parameter
return $Token
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment