Last active
September 29, 2025 14:45
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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