Get-AzureADUser -Filter "userPrincipalName eq '$upn'" not working

You mentioned in your initial post that this …

worked as expected.

Try to insert the userPrinicpalName of that particular user in my last code suggestion and run the code. … as it is.
Does that work?

The following line of code does work, I concur, it’s the input data that is the issue, or how the data is being reference in the variable.
Get-AzureADUser -Filter "userPrincipalName eq '$upn'"

I think @matt-bloomfield is correct and the $UPN value is likely null, which won’t work and will produce that exact error. Even if you updated the csv, you need to reload the CSV into your PS session to get the updated var so the property is correct.

Not to side track the discussion, but I’d also suggest switching this to graph if this is going to be for a process you are running for a while. AzureAD is planned for deprecation: AzureAD Module | Microsoft Learn

This can be accomplished similarly in graph by the following:
Get-MgUser -Filter "userPrincipalName eq '$upn'"

It’s also faster :slight_smile: but if you need specific properties you may have to ask for those if they aren’t returned by default.

2 Likes