# Get-ADuser dont work with varaible

**URL:** <https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940>\
**Category:** PowerShell Help\
**Created:** [July 21, 2022, 6:53am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940 "2022-07-21T06:53:30Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![LewisLH44](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@LewisLH44](https://forums.powershell.org/u/LewisLH44)\
**Post date:** [July 21, 2022, 6:53am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/1 "2022-07-21T06:53:30Z")

</div>

Hello, we have a lot of users in the Active Directory. With the following query I would like to narrow it down and query it a little more.

My problem is that it returns all users in spite of variables, instead of searching only those in the variable.

# Company

$VW = “XXX”

# Domain

$VWDomain = “[test.com](http://test.com)”

# Groups

$groups = “$VW1”, “$VW2”, “$VW3”, “$VW5”

#User catch  
$alluser=foreach ($group in $groups) {  
Get-ADGroupMember $group | select samaccountname  
}

#Here I get all user which are member of the groups before (around 8, perfect - thats what I want)  
$alluser

#Now I want to filter the 8 values, which are in the variable $alluser (user1, user2, user3,…). As soon as I do this, however, he searches me all users of the AD and I suddenly have 40 hits. I get now all user which got the domain [test.com](http://test.com), but I only want to look at the 8 hits.

$cleanuser = foreach ($user in $alluser) {  
Get-ADUser -filter “userPrincipalName -like ‘\*@$VWDomain’”  
}

$clearuser

---

<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:** [July 21, 2022, 7:12am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/2 "2022-07-21T07:12:57Z")

</div>

LewisLH44,  
Welcome to the forum. 👋🏽

That’s a common issue. Try it this way:

```auto
$VWDomain = '*@test.com'

Get-ADUser -Filter "userPrincipalName -like '$VWDomain'"

```

BTW: When you post code, sample data, console output or error messages please 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) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![LewisLH44](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@LewisLH44](https://forums.powershell.org/u/LewisLH44)\
**Post date:** [July 21, 2022, 7:27am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/3 "2022-07-21T07:27:54Z")

</div>

H, tanks for your fast reply 🙂 - I have changed that, unfortunately I get this error

```auto
Get-ADUser : Fehler beim Analysieren der Abfrage: "userPrincipalName -like *@test.com" Fehlermeldung: "syntax error" an folgender Position: "25".
In Zeile:19 Zeichen:3
+ Get-ADUser -Filter "userPrincipalName -like $VWDomain"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

```

---

<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:** [July 21, 2022, 7:37am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/4 "2022-07-21T07:37:52Z")

</div>

> [@LewisLH44](#):
>
> unfortunately I get this error

ah … ok … so we need the single quotes inside the filter string … changed my code suggestion above. Try now.

---

<div class="post-metadata">

**Author:** ![LewisLH44](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@LewisLH44](https://forums.powershell.org/u/LewisLH44)\
**Post date:** [July 21, 2022, 7:39am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/5 "2022-07-21T07:39:53Z")

</div>

Now it works, but I got the same Problem:

> $alluser=foreach ($group in $groups) {  
> Get-ADGroupMember $group | select samaccountname  
> }
> 
> $alluser

This shows me 8 user

user1  
…  
user8

```auto
$clearuser = foreach ($user in $alluser) {

  Get-ADUser -Filter "userPrincipalName -like $VWDomain"
}

$clearuser

```

And then I get all User of the AD with the domain, but I expected max. 8 from the variable

user1  
…  
user40

---

<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:** [July 21, 2022, 7:42am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/6 "2022-07-21T07:42:28Z")

</div>

> [@LewisLH44](#):
>
> And then I get all User of the AD with the domain, but I expected max. 8 from the varaible

You’re using a loop but you’re not using the loop variable. Actually this should be enough:

```auto
$clearuser = 
foreach ($user in $alluser) {
    Get-ADUser -Identity $($user.sAMAccountName)
}
  
$clearuser

```

---

<div class="post-metadata">

**Author:** ![LewisLH44](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@LewisLH44](https://forums.powershell.org/u/LewisLH44)\
**Post date:** [July 21, 2022, 7:44am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/7 "2022-07-21T07:44:50Z")

</div>

```auto
Get-ADUser : Das Argument für den Parameter "Identity" kann nicht überprüft werden. Die Identitätseigenschaft des Arguments ist NULL oder leer.
In Zeile:19 Zeichen:24
+ Get-ADUser -Identity $user
+ ~~~~~
    + CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

```

But I need a filter for the 8 hits? ☹

---

<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:** [July 21, 2022, 7:50am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/8 "2022-07-21T07:50:11Z")

</div>

> [@LewisLH44](#):
>
> But I need a filter for the 8 hits?

Ah … ok … now I see … sometimes it’s hard to see it here in the forum … I changed my last code suggestion. We have to use a subexpression. 😉

---

<div class="post-metadata">

**Author:** ![LewisLH44](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@LewisLH44](https://forums.powershell.org/u/LewisLH44)\
**Post date:** [July 21, 2022, 7:54am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/9 "2022-07-21T07:54:04Z")

</div>

> [@Olaf](#):
>
> ```auto
> $clearuser = 
> foreach ($user in $alluser) {
> Get-ADUser -Identity $($user.sAMAccountName)
> }
>   
> $clearuser
> 
> ```

No Problem , Im happy that you support me 🙂

And now in need for the loop a filter, that says only user (of the 8 hits) who got the domain [test.com](http://test.com)

For Example:  
user1 got as userPrincipalName [test@test.com](mailto:test@test.com)  
user4 got as userPrincipalName test@123

So user 4 should remove from $clearuser

---

<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:** [July 21, 2022, 7:55am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/10 "2022-07-21T07:55:12Z")

</div>

> [@LewisLH44](#):
>
> And now in need for the loop a filter, that says only user (of the 8 hits) who got the domain [test.com](http://test.com)

Use a `Where-Object`!? 🤷🏽‍♂️ 😉 🙂

---

<div class="post-metadata">

**Author:** ![LewisLH44](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@LewisLH44](https://forums.powershell.org/u/LewisLH44)\
**Post date:** [July 21, 2022, 8:01am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/11 "2022-07-21T08:01:23Z")

</div>

`Get-ADUser -Identity $($user.sAMAccountName) | Where-Object "userPrincipalName -like $VWDomain"`

I think that is not working ☹

---

<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:** [July 21, 2022, 8:05am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/12 "2022-07-21T08:05:12Z")

</div>

> [@LewisLH44](#):
>
> I think

??? Did you try?

Please always read the help including the examples for the cmdlets you’re about to use to learn how to use them.

> [@LewisLH44](#):
>
> is not working

Because the syntax is wrong.

> **[Where-Object (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=powershell-7.2)**
>
> The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it. For example, you can use the Where-Object cmdlet to select files that were created after a certain date, events with a...

---

<div class="post-metadata">

**Author:** ![LewisLH44](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@LewisLH44](https://forums.powershell.org/u/LewisLH44)\
**Post date:** [July 21, 2022, 8:09am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/13 "2022-07-21T08:09:59Z")

</div>

Thanks, I opened the site already, but still dont get 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:** [July 21, 2022, 8:34am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/14 "2022-07-21T08:34:12Z")

</div>

> [@LewisLH44](#):
>
> but still dont get it

For `Where-Object` you always have several options. If you have only one condition you could do this:

```auto
Get-ADUser -Identity $($user.sAMAccountName) | 
    Where-Object -Property 'userPrincipalName' -Like -Value $VWDomain

```

or like this:

```auto
Get-ADUser -Identity $($user.sAMAccountName) | 
    Where-Object {$_.userPrincipalName -Like $VWDomain}

```

The latter one would be the option to go with when you have more than one condition to check.

---

<div class="post-metadata">

**Author:** ![LewisLH44](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@LewisLH44](https://forums.powershell.org/u/LewisLH44)\
**Post date:** [July 21, 2022, 8:59am UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/15 "2022-07-21T08:59:32Z")

</div>

I got it, Thank you 🙂

---

<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:17pm UTC](https://forums.powershell.org/t/get-aduser-dont-work-with-varaible/19940/16 "2024-05-16T20:17:40Z")

</div>


