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

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

  1. Pingback: Don’t do that #22: Manage a remote server using RDP | Powershell Guru

  2. Pingback: Don’t do that #24: Use Get-QADUser (Quest) to receive AllMemberOf of a user | Powershell Guru

Leave a Reply

Your email address will not be published. Required fields are marked *