Cannot filter Get-ADUser output with Where-Object


I’ve encountered a problem and after half a day faffing about i’m still stumped.

So the following code works:

Get-ADUser -filter {my-custom-attribute -eq 6} -SearchBase 'OU=Users,DC=my,DC=local'

And this does not:

Get-ADUser -filter * -SearchBase 'OU=Users,DC=my,DC=local' `
| Where-Object {$_."my-custom-attribute" -eq 6}

The first code returns a list of all users where my-custom-attribute is equal to 6; the second code returns nothing. If i use a property like samaccountname in the second example it returns what i would expect. So at this point, after much fiddling about, i think my problem might be down to a fundamental misunderstanding of how the $_ variable works but it’s hard to tell!

I would very much appreciate a shove in the right direction.


my-custom-attribute is not in the default property output set for Get-ADUser. You will need to add -Properties ‘my-custom-attribute’ to your command before piping to Where.

Also, recommend avoiding the accent character. Finish the line with a pipe to indicate pipeline continuation

Get-ADUser -filter * -SearchBase 'OU=Users,DC=my,DC=local' -Properties 'my-custom-attribute' | 
    Where-Object {$_."my-custom-attribute" -eq 6}

Thanks chaps that worked a treat!