Check a user exist in Azure Ad using MS Graph

Tanmoy Das 806 Reputation points
2022-04-17T02:05:41.55+00:00

Hi,

I am trying to confirm whether a user exists or not in Azure Ad using Graph api in PowerShell.
Any thoughts?

Will this help

$resource=https://graph.microsoft.com/beta/

$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody

$userapiUrl = "$resource/users('userprincipal name')"
$response = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $userapiUrl -Method Get

if($response)
{

user exists

}
else
{

user does not exist

}

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,540 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. James Hamil 26,131 Reputation points Microsoft Employee
    2022-04-18T19:49:11.197+00:00

    Hi anonymous user , thank you for your question. To check if a user exists or not, I would first recommend using the Graph Explorer. This way you can test your queries to make sure they work.

    For your question, I recommend using this reference to get a user. And if the response is null, you simply have no user. Just like you have in your example. I would sign in to the graph explorer and test your query. This way you can use your own directory instead of the sample data provided.

    Please let me know if you have any questions and I can help you further!

    If this answer helped you please mark it as "Verified" so other users can reference it.

    Thank you,
    James

    0 comments No comments

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.