This article explains how to easily add additional headers to the Graph API calls you make in PowerShell using Invoke-PnPGraphMethod commandlet. There’s a convenient sample for you to copy-paste and some basic explanations on what we’re doing below.
Table of Contents
Every now and then, I need to make some Graph API calls from PowerShell. And there’s a convenient PowerShell commandlet available to make it easy to do just that – Invoke-PnPGraphMethod.
But to make some of the calls successful, you need to add some additional headers. Yes, “consistencyLevel: eventual”, I’m looking at you.
Again, Invoke-PnPGraphMethod is our friend. It has an additional parameter called “AdditionalHeaders” that accepts a string dictionary of headers – we just need to know the right way to add them.
But how do you do that with PowerShell?
Let’s not beat around the bush. The solution is very straightforward, and here it is in copy-pasteable format:
$url = "https://graph.microsoft.com/beta/users?" $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("consistencyLevel", "eventual"); $result = Invoke-PnPGraphMethod -Url $url -Method "Get" -AdditionalHeaders $headers
… and because search engines need to be satisfied, here’s a bloated how-to on, well, how to do this and why:
Time needed: 2 minutes
How to supply Additional Headers (like consistencyLevel: eventual) to Invoke-PnPGraphMethod PowerShell commandlet?
- Create a string Dictionary object to hold our additional headers
Create a new instance of the
System.Collections.Generic.Dictionaryclass and assigned it to the variable
This dictionary will be used to store additional headers that need to be included in the API request.
- Add your desired headers to the Dictionary
Now we can add our headers. I’m going with a true classic – consistencyLevel: eventual. See below.
This line adds a key-value pair to the
$headersdictionary. The key
"consistencyLevel"is associated with the value
"eventual". These headers are used to provide additional information or configuration options for the API request. In this case, it sets the consistency level to “eventual,” indicating that eventual consistency is acceptable for the response data.
This is required for a lot of Graph API methods.
- And finally, call the Graph
Now we are ready to execute our Graph call!
Invoke-PnPGraphMethod -Url $url -Method "Get" -AdditionalHeaders $headers
This line invokes the
Invoke-PnPGraphMethodfunction from the PnP PowerShell module. It sends an HTTP GET request to the specified URL (
$url) with the provided additional headers (
This code sets up the necessary parameters and headers and then makes a request to the Microsoft Graph API to retrieve user data using the beta version of the API.
- How to find out dependency versions in your .csproj files using PowerShell? - September 26, 2023
- node build throwing “error:0308010C:digital envelope routines::unsupported”? Let’s fix it! - September 19, 2023
- “This app can’t run on your PC” dialog when you’re building your app in Visual Studio - September 12, 2023