How would you query for all AD users with names that start with a “e”-“g”? You shouldn’t use a client-side filter such as Where-Object. One thing you can do is use the -Filter parameter with logical operators such as -and and -or:

Get-ADUser -filter {(name -lt 'E') -or (name -gt 'G')} |
  Select-Object -ExpandProperty Name

this example requires the free RSAT tools from Microsoft to be installed)

Twitter This Tip! ReTweet this Tip!

Anonymous
  • This filter is incorrect.  It actually pulls all names before E, and all names after "G", except the one named "G"
    This filter would be more accurate to get all of E*, F*, and G* names.
    Get-ADUser -filter {(name -ge 'E') -and (name -lt 'H')} |
     Select-Object -ExpandProperty Name
    Thanks!