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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay