Don’t do that #23: Use Where-Object to search a user with filter (like)

By | April 23, 2015

Don’t do that: The following code performs a search for users inside an OU where the name contains “vip”.

(Get-ADUser -SearchBase 'OU=myOU,DC=domain,DC=com' -Filter * | ?{$_.Name -match 'vip'}).Name


Do that: The above code works, but I don’t like because there is no filter applied, it loads in memory useless objects.

In this specific scenario, try to avoid the pipeline (filter before, not after).

Inside very large OU with thousand of users, using Where-Object (alias “?”) will be slower than using -Filter or -LDAPFilter :

Note: In this case, the search will return only the Property “Name”.


previous-buttonNext button blue

Leave a Reply

Your email address will not be published.