How to Output Invoke-MgGraphRequest Responses as a PowerShell Object

Invoke-MgGraphRequest with Microsoft Graph PowerShell is a flexible cmdlet that allows you to make almost any call to the Microsoft Graph API and acts as a sort of catch-all for areas the Microsoft Graph PowerShell SDK doesn’t cover. For a more detailed write-up on specifically how to use the command, check out my post here.

One of the issues with this command, although in most instances it is useful, is that by default when you make a GET request to Microsoft Graph, your response will be returned to you in a hashtable format which isn’t as useful as other available formats. Let’s look at how we can get responses in a more manageable format, such as PowerShell Objects!

Checking the response type with '.GetType()'

First, to look at what a default response to a GET request looks like in PowerShell, we can evaluate the data type. Here I am going to connect to Microsoft Graph PowerShell and make a GET call to the /v1.0/me endpoint, this will retrieve basis user information for the currently logged in user.


$dflt= Invoke-MgGraphRequest -uri "" -method GET

Now our response is saved within our variable, by appending .GetType() to that variable we can check the data type. 


Our response details the name of the object type, which in this case is a Hashtable:

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                                                                                                                                         
-------- -------- ----                                     --------                                                                                                                                                                                                                                                                                                         
True     True     Hashtable                                System.Object   

Hashtables can be hard to interrogate with PowerShell, especially if you are a non-developer like me you may struggle to work with the default response type. For example, if you wanted to view the value of a single property through dot notation, you can only display the hash table keys or values.

Displaying the dot notation of a hash table
Displaying the dot notation of a hash table

A simpler solution (but maybe less efficient) is to change the response type to that of a PowerShell Object, which is generally easier to work with.

Changing the output type with the '-OutputType' parameter

The -OutputType parameter allows you to change how (what type) the data is returned to you in your PowerShell session. The parameter supports four different types, including:

  • HashTable
  • HttpResponseMessage
  • Json
  • PSObject

The most useful output type when working with PowerShell is the PSObject type (or at least it is for the average administrator). When using PowerShell, objects are generally easier to work with, data is represented more clearly and it is well structured. You will also find it is easier to convert and export said data as a CSV file, which is commonly used for generating reports. Instead, if you attempted to export a hashtable as a CSV, the key/value pairs will not display as they should. 

We can append “-OutputType PSObject” to our above example to get the desired result:

$psobj = Invoke-MgGraphRequest -uri "" `
-method GET -OutputType PSObject

Let’s verify the data type by appending .GetType() to our variable:


IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                                                                                                                                         
-------- -------- ----                                     --------                                                                                                                                                                                                                                                                                                         
True     False    PSCustomObject                           System.Object 

Notice below how the response differs from the previous example and we can use dot notation to view specific properties.

PSObject response
PSObject response

Wrapping up

While hashtables have their benefits, for Microsoft 365 administrators who need to interpret data in a way that they may already be familiar with, the PSObject type will give them the best experience. Once you have your data as a custom object, you will be able to interact with it in the same way as you would with responses from other SDK cmdlets such as Get-MgUser, for example.

Daniel Bradley

My name is Daniel Bradley and I work with Microsoft 365 and Azure as an Engineer and Consultant. I enjoy writing technical content for you and engaging with the community. All opinions are my own.

Leave a Reply