Microsoft Graph Object Pagination and Ordering When Working in PowerShell SDK

Blog by: Dmitry Sotnikov

Find him on LinkedIn or Twitter.

Question from Justin during our webinar, “Microsoft Graph Basics for PowerShell Admins”:

  • Is Graph data sorted in some way so TOP returns the same value every time, or will it just return the first object that happens to be returned?

 

Microsoft Graph implements OData pagination in order to return large amounts of data in chunks. You can read more about that process at https://docs.microsoft.com/en-us/graph/paging

You can use the $top and $skip parameters in Graph URLs to specify how many objects you want to retrieve and at which object you want to start, for example: https://graph.microsoft.com/v1.0/users?$top=2

In PowerShell, these translate to the -Top and -Skip parameters of all cmdlets. For example, you could do the following to get just two users:

Get-MgUser -Top 2

By the way, note that not all Microsoft Graph APIs and thus not all cmdlets fully support all query parameters, and in the example above (as of right now) -Skip will not work.

Microsoft does not guarantee that objects returned by the APIs will be ordered in any specific way. Thus, we recommend that when you care about object order, you explicitly set it with the $orderby URL parameter or -OrderBy PowerShell parameter respectively. For example:

https://graph.microsoft.com/v1.0/users?$orderby=displayName%20DESC

Or

Get-MgUser -OrderBy displayName

Note that if you want descending order, you need to pass the desc parameter within quotes as shown below:

Get-MgUser -OrderBy “displayName desc”

For more, watch the on-demand webinar, Microsoft Graph Basics for PowerShell Admins.

Check out these relevant resources.

New Survey Finds...

Active Directory forest recovery not taken serious enough. See what else your peers had to say.