Using AD Filters with Cmdlets (Part 1)

by Jul 31, 2018

The free RSAT tools come with the ActiveDirectory PowerShell module. You can use the cmdlets from this module to retrieve AD information such as user or group names. Cmdlets like Get-ADUser and Get-ADComputer support server-side filters. They work a bit different than you might think, though.

For simple queries, these filters are easy to use. For example, this line gets you the first 5 users with names that start with „A“. and the filter syntax looks almost like PowerShell code:

 
Get-ADUser -Filter { name -like 'A*' } -ResultSetSize 5 
 

That’s not true though: the –Filter parameter requests plain text, so you could as well use quotes instead of braces:

 
Get-ADUser -Filter " name -like 'A*' " -ResultSetSize 5
 

Using braces (a script block) is still a good idea because braces capture PowerShell code, so you get color-coding and syntax errors when you write code inside of braces. Script blocks can easily be converted to strings later (which is what Get-ADUser does automatically).

Twitter This Tip! ReTweet this Tip!