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

**URL:** <https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969>\
**Category:** PowerShell Help\
**Created:** [October 11, 2023, 11:32am UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969 "2023-10-11T11:32:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 11:32am UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/1 "2023-10-11T11:32:02Z")

</div>

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’”
- 

```auto
  + CategoryInfo : NotSpecified: (:) [Get-AzureADUser], ApiException
  + FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.GetUser
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 11, 2023, 12:00pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/2 "2023-10-11T12:00:14Z")

</div>

Andrey,  
Welcome to the forum. 👋🏼

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

> [@Andrey\_Zdasena](#):
>
> C:\Users\Andrejs.Zdasena\> $userlist = Import-Csv C:\Temp\users

Shouldn’t there be a proper file name?

How does you input CSV look like?

---

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 12:08pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/3 "2023-10-11T12:08:11Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/d/dca2a2c4ec10d210fb6f81aa80c67bca7216ce0b.png)

---

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 12:09pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/4 "2023-10-11T12:09:29Z")

</div>

ok thanks i will use this brackets \</\> next time 🙂

---

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 12:14pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/5 "2023-10-11T12:14:07Z")

</div>

full code is looks like

```auto
 $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"
 }
}

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 11, 2023, 12:55pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/6 "2023-10-11T12:55:42Z")

</div>

> [@Andrey\_Zdasena](#):
>
> `$user = Get-AzureADUser -ObjectId "userPrincipalName eq '$upn'"`

`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.

---

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 1:01pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/7 "2023-10-11T13:01:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 11, 2023, 1:30pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/8 "2023-10-11T13:30:26Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 1:34pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/9 "2023-10-11T13:34:58Z")

</div>

I get this kind of error

```auto
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

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 11, 2023, 1:43pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/10 "2023-10-11T13:43:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 1:48pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/11 "2023-10-11T13:48:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 11, 2023, 1:49pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/12 "2023-10-11T13:49:51Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 1:59pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/13 "2023-10-11T13:59:21Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/0/023372c8dcbaa1af03fd170e3209f2e542982f18.png)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 11, 2023, 2:01pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/14 "2023-10-11T14:01:17Z")

</div>

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 …

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

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

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 11, 2023, 2:01pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/15 "2023-10-11T14:01:32Z")

</div>

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:

```auto
$upn = $line.UserPrincipalName

```

to

```auto
$upn = $line.UPN

```

---

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 2:02pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/16 "2023-10-11T14:02:28Z")

</div>

i did also with column name UserPrincipalName and the same error

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 11, 2023, 2:03pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/17 "2023-10-11T14:03:06Z")

</div>

> [@matt-bloomfield](#):
>
> 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:

The first screenshot still had “userPrincipalName”

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

---

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 2:03pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/18 "2023-10-11T14:03:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 11, 2023, 2:04pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/19 "2023-10-11T14:04:45Z")

</div>

OK, but now you have two people confirming that you’re code actually works. 🤷🏼‍♂️  
I tested it as well and it works as expected.

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

---

<div class="post-metadata">

**Author:** ![Andrey\_Zdasena](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Andrey\_Zdasena](https://forums.powershell.org/u/Andrey_Zdasena)\
**Post date:** [October 11, 2023, 2:14pm UTC](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969/20 "2023-10-11T14:14:19Z")

</div>

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

[Next page](https://forums.powershell.org/t/get-azureaduser-filter-userprincipalname-eq-upn-not-working/22969.md?page=2)
