Azure DevOps API is not returning all users - Only the First 498 users are returned

Anonymous
2024-05-22T12:01:58.0033333+00:00

The below code is fetching only the first 498 users. How to fetch all the users from Azure DevOps

Variables

$organization = ""

$pat = ""

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))

$apiVersion = "7.1-preview.1"

$users = @()

$url = "https://vssps.dev.azure.com/$organization/_apis/graph/users?api-version=$apiVersion"

$cnt = 1

Function to fetch users

function Get-Users {

param (

    [string]$url,

    [hashtable]$headers

)

$response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers

$global:users += $response.value



# Check for continuation token and fetch next page if it exists

if ($response.Headers.'x-ms-continuationtoken') {

    $continuationToken = $response.Headers.'x-ms-continuationtoken'

    $nextUrl = "https://vssps.dev.azure.com/$organization/_apis/graph/users?api-version=$apiVersion&continuationToken=$continuationToken"

    Get-Users -url $nextUrl -headers $headers

}

}

Initial headers

$headers = @{

Authorization = "Basic $base64AuthInfo"

}

Fetch all users

Get-Users -url $url -headers $headers

Output all users

$users | ForEach-Object {

Write-Output $_

Write-Output "User Cnt:" $cnt

$cnt = $cnt + 1

}

Write-host "Total Users - " $users.Count

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. David Joseph 0 Reputation points
    2025-11-03T16:45:52.29+00:00

    Hi there, I had the same issue, The answer is to retrieve the value from the "X-MS-ContinuationToken" response header from the first call (with 498 users) and send a second call with this value in the querstring for "continuationToken" i.e.

    https://{{ado-graph-url}}/{{ado-organization}}/_apis/graph/users?api-version={{api-version}}&continuationToken=valueFromX-MS-ContinuationToken

    if you need more then you would rinse and repeat until you dont receive a "X-MS-ContinuationToken" response header

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.