# AzureADuser foreach question

**URL:** <https://forums.powershell.org/t/azureaduser-foreach-question/15418>\
**Category:** PowerShell Help\
**Created:** [December 21, 2020, 7:53pm UTC](https://forums.powershell.org/t/azureaduser-foreach-question/15418 "2020-12-21T19:53:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [December 21, 2020, 7:53pm UTC](https://forums.powershell.org/t/azureaduser-foreach-question/15418/1 "2020-12-21T19:53:12Z")

</div>

Hi All

the goal of this to retrieve the objectID from a disabled user and to remove this user from all the azureAD groups.  
In order to do this I need to get the object from the user first

when using this as a standalone command

```
get-AzureAdUser -SearchString "SamaccountName" |select objectId, Displayname, UserPrincipalName |export-csv c:\temp\AzureAdUsers.csv -notypeinformation -append
```

it works like a charm  
however when I want to use it in a foreach I get the below mentioned error

```
$users = import-csv C:\users\Public\Documents\CSV\ToRemove.csv
foreach($user in $users){
get-azureAdUser -SearchString "$user.SamAccountName" |select Objectid,displayname,UserPrincipalName |export-csv c:\temp\azureADusers.csv -NoTypeInformation -append
}
```

and the error is this  
get-azureAdUser : Error occurred while executing GetUsers  
Code: Request\_UnsupportedQuery  
Message: Unsupported or invalid query filter clause specified for property ‘userPrincipalName’ of resource ‘User’.  
any Idea on how to get this sorted out?

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [December 21, 2020, 9:47pm UTC](https://forums.powershell.org/t/azureaduser-foreach-question/15418/2 "2020-12-21T21:47:37Z")

</div>

Only a base variable can be expanded in a string. Since you need to reference the samaccountname property surround it with a subexpression. So change

“$user.samaccountname”

to

”$($user.samaccountname)”

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [December 21, 2020, 11:46pm UTC](https://forums.powershell.org/t/azureaduser-foreach-question/15418/3 "2020-12-21T23:46:46Z")

</div>

THanks Doug,  
an additional question that pops in my mind. A user can be member in multiple groups and this will as such also be multiple times populated in my export is there a way that I Can use distinct to just use a given user.objectid once? in the following code

```
$azureadgroups = Get-AzureADGroup
$users = import-csv c:\temp\azureaduser.csv

foreach($user in $users){
        foreach($group in $azureadgroups){
    
        try{
        Remove-AzureADGroupMember -objectID $group.ObjectId -memberID $user.objectid
        }
        Catch{
        write-warning "$_"
      }
   }
}
```

thanks again for your help

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/azureaduser-foreach-question/15418/4 "2024-05-16T20:28:09Z")

</div>


