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 :
1 2 3 4 5 6 7 8 9 |
# Filter (Get-ADUser -SearchBase 'OU=myOU,DC=domain,DC=com' -Filter {name -like '*vip*'} -Properties Name).Name # LDAPFilter (Get-ADUser -SearchBase 'OU=myOU,DC=domain,DC=com' -LDAPFilter '(name=*vip*)' -Properties Name).Name # With a variable $user = '*vip*' (Get-ADUser -SearchBase 'OU=myOU,DC=domain,DC=com' -Filter {name -like $user} -Properties Name).Name |
Note: In this case, the search will return only the Property “Name”.