I can't fetch all Azure AD users through Microsoft Graph

Federico Coppola 1,181 Reputation points
2023-04-07T16:04:49.4433333+00:00

Hello,
I would create a list Azure AD CSV file. I am trying to use Microsoft Graph API to fetch data.

$tenantID = "XXX"
$applicationID = "XXX"
$clientKey = "XXX"
 
$url = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$resource = "https://graph.microsoft.com/"
$restbody = @{
         grant_type    = 'client_credentials'
         client_id     = $applicationID
         client_secret = $clientKey
         resource      = $resource
}
     
# Get the return Auth Token
$token = Invoke-RestMethod -Method POST -Uri $url -Body $restbody
     
# Set the baseurl to MS Graph-API (v 1.0)
$baseUrl = 'https://graph.microsoft.com/v1.0'
         
# Pack the token into a header for future API calls
$header = @{
          'Authorization' = "$($Token.token_type) $($Token.access_token)"
         'Content-type'  = "application/json"
}
 
# Build the Base URL for the API call
$url = $baseUrl + '/users'
  
# Call the REST-API
$usersList = Invoke-RestMethod -Method GET -headers $header -Uri $url
$usersList.value |Where-Object { $_.mobilePhone -ne $Null } | Format-Table displayName,givenName,surname,mobilePhone,jobTitle

At the moment I got data via PowerShell, however user list is not complete.
I don't see all Azure AD user via PowerShell.
How can I solve? Any suggestion for me?

Thanks
Federico

Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Graph
{count} votes

Accepted answer
  1. HarmeetSingh7172 4,826 Reputation points
    2023-04-07T22:45:38.6766667+00:00

    Hello Federico Coppola,

    Thanks for reaching out! I agree with @Patchfox on this. Some queries against Microsoft Graph return multiple pages of data either due to server-side paging or due to the use of the $top query parameter to specifically limit the page size in a request. When more than one query request is required to retrieve all the results, Microsoft Graph returns an @odata.nextLink property in the response that contains a URL to the next page of results. Further reading on Paging-MS graph. Note that the script is currently configured to retrieve only the first 5 users by setting the $top parameter to 5 in the initial Graph API request. This value can be adjusted as needed to retrieve more or fewer users. Refer below documentation to know how to use query parameters to control the amount of data in response:
    https://learn.microsoft.com/en-us/graph/query-parameters?tabs=http Hope this helps. If the answer is helpful, please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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