DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

1

Get AAD AccessToken via PowerShell with user password

I needed to get Azure AD Access Token by using hard-coded username and password. To do so, we can use grant_type=password.

Prerequisites

I registered an application to Azure AD to user for this.

PowerShell script

I used following PowerShell script to get access token. As I didn't want to user MSAL as part of script, so I simply use Invoke-RestMethod.

$body = 'grant_type=password' + `
'&client_id=<client id>' + `
'&username=<user>@<yourdomain>.onmicrosoft.com' +`
'&password=<user password>' +`
'&resource=<resource i.e. https://graph.microsoft.com>' +`
'&client_secret=<client secret>' +`
'&scope=<score i.e. User.Read>'
$token = (Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/common/oauth2/token -Body $body).access_token
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay