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.
Background
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?
Solution
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.Dictionary
class and assigned it to the variable$headers
.
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.
$headers.Add("consistencyLevel", "eventual")
This line adds a key-value pair to the$headers
dictionary. 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 theInvoke-PnPGraphMethod
function from the PnP PowerShell module. It sends an HTTP GET request to the specified URL ($url
) with the provided additional headers ($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.
- Long Path Tool – an unfortunate review - March 3, 2025
- Copilot Android app refusing to take pictures? I have the dumbest fix you’ll ever hear, but it’ll work, trust me. - February 26, 2025
- Adventures in the Windows 11 24H2-land: How to restore your webcam after February 2025 Patch breaks it? - February 25, 2025