How to use REST API with Powershell

  • last updated: Fri, 10 Feb 2023 14:02:11

The past few years REST API became important more and more. REST API is a common way for communicating against and between applications. Azure and Microsoft 365 also using REST API’s.
As the world of API is growing so the tooling for querying API’s is growing with the most well known tool is Postman.

But what if you need API output for automation purposes, Postman isn’t the tool you need. So in my case I use Powershell.

Register an app in Azure AD

If you use Powershell you need a couple of things before you can send an authentication request. First of all you need to register an app into the Azure Active Directory. How to accomplish this goal manual you can take a look at the following url: you want to register an application automated with code you can check my post about application registration automated.

After the application is finished and you have set an application secret you can use the applicationId and secret in PowerShell.


Get a token in Powershell

Now the needed application is finished we can start with the Powershell part. Good to know is there are some different Microsoft parts where you can authenticate with.
From Azure perspective most of the time you need the Graph API so you can authenticate with
When you want to query Microsoft 365 you will need

In Powershell the code looks like this

$resource = "https//"
$tenantId = "the tenant you want to authenticate to"
$clientId = "the application id"
$clientSecret = "The secret you have created under secrets"
$body = @{grant_type = "client_credentials"; resource = $resource; client_id = $ClientId; client_secret = $ClientSecret }

$oauth = Invoke-RestMethod -Method Post -Uri "$($tenantID)/oauth2/token?api-version=1.0" -Body $body
$token = @{'Authorization' = "$($oauth.token_type) $($oauth.access_token)" }

Whats next

Now you have a token in the $token variable. After then you can query against API’s. For example the Microsoft 365 ServiceHealth. In this case i’ve used the GET method.

$uri = "$($tenantId)/ServiceComms/CurrentStatus"
Invoke-RestMethod -Uri $uri -Headers $token -Method Get

Some other common used methods are POST (create a new record) or PATCH (partial updating records)
You can find a whole explanation about REST API’s at

Thank you for reading my blog how to use rest api with powershell.
I hope you got a bit inspired. Enjoy your day and happy automating 👋

comments powered by Disqus

Related Posts

The IntuneCLI for automated Intune management

Managing Intune can be challenging, especially when having multiple Microsoft Intune environments. Because of that, I started project IntuneAssistant. A part of the project is a CLI tool that can run as a daemon in your own environment.

Read more

Update Intune filters with Windows 11 23H2 build automated

As you also may know is that using filters in Intune is a best practise. With the come of the Windows 11 23H2 build number it is a good idea 💡 to update your filters by adding the new 10.

Read more

Windows Store (winget) App Not Supported in Preview in Intune

The new Windows Store App feature (aka WinGet installation) becomes more popular. However, this feature is still in preview. That means that not all is working as expected.

Read more