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

C:\Users\Andrejs.Zdasena> $userlist = Import-Csv C:\Temp\users

foreach ($line in $userlist) {
$upn = $line.UserPrincipalName
Get-AzureADUser -Filter “userPrincipalName eq ‘name.surname@domain’”
}

this from is working but if i use

C:\Users\Andrejs.Zdasena> $userlist = Import-Csv C:\Temp\users

foreach ($line in $userlist) {
$upn = $line.UserPrincipalName
Get-AzureADUser -Filter “userPrincipalName eq ‘$upn’”
}
then don’t work, what is the reason, compilation says that

Get-AzureADUser : Error occurred while executing GetUsers
Code: Request_UnsupportedQuery
Message: Unsupported or invalid query filter clause specified for property ‘userPrincipalName’ of resource ‘User’.
RequestId: 28632614-1c12-4cba-8f2c-5a838783cf5d
DateTimeStamp: Wed, 11 Oct 2023 11:31:44 GMT
HttpStatusCode: BadRequest
HttpStatusDescription: Bad Request
HttpResponseStatus: Completed
At line:4 char:1

  • Get-AzureADUser -Filter “userPrincipalName eq ‘$upn’”
  •   + CategoryInfo          : NotSpecified: (:) [Get-AzureADUser], ApiException
      + FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.GetUser

Andrey,
Welcome to the forum. :wave:t3:

It is hard to distinguish between text, code and error message because you did not format your code as code.

So please when you post code, sample data, console output or error messages format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Shouldn’t there be a proper file name?

How does you input CSV look like?

yes in the end i forgot to write .csv and csv is looks like

ok thanks i will use this brackets </> next time :slight_smile:

full code is looks like

 $userlist = Import-Csv C:\Temp\Users.csv
foreach ($line in $userlist) {
$upn = $line.UserPrincipalName
 $user = Get-AzureADUser -ObjectId "userPrincipalName eq '$upn'"
 if ($user) {
 try{
$user | Set-AzureADUser -Department $line.department -mobile $line.mobilephone
} catch {

 Write-Warning "$upn user found, but FAILED to update."
 }
 }
 else{
Write-Warning "$upn not found, skipped"
 }
}

ObjectId does not support a comparison operator. It should be the UPN, or the ObjectId.

If you want to use a comparison operator, you should use the -Filter parameter.

firstly i use -Filter and it doesn’t work too, after i tried objecID just for test

The code works fine for me. If you output the UPN, do you see valid data?

$userlist = Import-Csv C:\Temp\Users.csv
$userlist.UserPrincipalName

I get this kind of error

Get-AzureADUser : Error occurred while executing GetUsers
Code: Request_UnsupportedQuery
Message: Unsupported or invalid query filter clause specified for property 'userPrincipalName' of resource 'User'.
RequestId: be78e10b-3638-44d3-8111-6241b10f840d
DateTimeStamp: Wed, 11 Oct 2023 13:33:36 GMT
HttpStatusCode: BadRequest
HttpStatusDescription: Bad Request
HttpResponseStatus: Completed
At line:4 char:10
+  $user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-AzureADUser], ApiException
    + FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.GetUser

Sure, I saw that. But if the column name isn’t actually called ‘UserPrincipalName’ then you will get that error. That’s why I asked you to validate the data.

Yes, i tried to change column name on upn and run that script it has the same error

What does the CSV file look like? What is the output of?

$userlist = Import-Csv C:\Temp\Users.csv
$userlist.UserPrincipalName

the idea is that i create csv file with info that i want enter automatically into the AzureAD, in Azure AD is users but they don’t have some information that i want add from csv file. like department, mobilephone, and csv file basically has column names and data.

Just to make sure that it’s not an issue with your input data I’d check it with one particular user account where you’re sure that it exists …

$CSVInputData = @'
userprincipalname,mobilePhone
"user@principal.name","01928340918234"
'@ |
    ConvertFrom-Csv

foreach ($line in $CSVInputData) {
    Get-AzureADUser -Filter "userPrincipalName eq '$($line.UserPrincipalName)'"
    }

Your CSV has the column UPN. You either need to rename that to UserPrincipalName and keep your script as it is or change your script from:

$upn = $line.UserPrincipalName

to

$upn = $line.UPN
1 Like

i did also with column name UserPrincipalName and the same error

The first screenshot still had “userPrincipalName”

@Andrey_Zdasena
You should use consistent data. Otherwise it is impossible to actually help you. :smirk:

yes i had correct info, just for example i change it

OK, but now you have two people confirming that you’re code actually works. :man_shrugging:t3:
I tested it as well and it works as expected.

Try my last code suggestion. It does not involve external data.

it is strange it says that not found upn, but i checked that users in list is also on AzureAD