i’m trying to execute with powershell follow query:

$upn = "[email protected]"
$URL = "https://graph.microsoft.com/v1.0/users?$filter=mail eq '$upn'"

#Send Request
Invoke-RestMethod -Method GET -Uri $URL -Headers $headers

command run but does not return the user in the query but the list of first 100 users. On ms doc the url is that one. What is wrong?

The service principal that i’m using have user.readwrite.all permission

1

Simple issue, you have to escape the $ dollar sign in your URL.

The code:

$upn = "[email protected]"
$URL = "https://graph.microsoft.com/v1.0/users?$filter=mail eq '$upn'"

When you look at the contents of the $URL variable:

PS C:> $URL
https://graph.microsoft.com/v1.0/users?=mail eq '[email protected]'

You will notice that the $filter is missing, and therefore the API will return all the users. PowerShell is interpreting the string and expanding the $filter variable for you… even if that variable is empty/doesn’t exist and you didn’t mean for it to be interpreted as a variable. You can simply add a back tick character (`) before $filter to escape:

$upn = "[email protected]"
$URL = "https://graph.microsoft.com/v1.0/users?`$filter=mail eq '$upn'"

1