Home > Software engineering >  Using Variables in Powershell Get-ADUser -Filter
Using Variables in Powershell Get-ADUser -Filter

Time:09-17

Can someone assist me on proper quoting, I need the wild cards beside the varibles for first and last name in a Get-ADUser -Filter search

I believe I need to escape the single quotes but can't get a successful return.

$LastADname = John
$FistADname = Doe
Get-ADUser -Filter "Name -like `*$LastADname`*$FirstADname`*"

Returns: Get-ADUser : Error parsing query: 'Name -like *Doe*John*' Error Message: 'syntax error' at position 12

This lets me know it did resolve the variable but not the *. If I wrap all in single quote to double quote the variables and *'s it won't resolve variables. Like below

$LastADname = John
$FistADname = Doe
Get-ADUser -Filter 'Name -like "*$LastADname*$FirstADname*"'

I belive this becusae the above resolves but no vaule produced. When I substite the var for text it produces results

Get-ADUser -Filter 'Name -like "*Doe*John*"'

CodePudding user response:

You need to wrap the internal filter string with single quotes:

Get-ADUser -Filter "Name -like '*$LastADname*$FirstADname*'"

You don't need the ` to escape the * character. The second attempt with single-quotes ' wrapping the outer string didn't work because single-quoted strings are rendered literally. Double-quoted " strings allow you to expand variables, use escape-sequences with `, and return sub-expressions within a string.


However, if the target field value contains a ' (or may contain, if controlled by a variable), such as with the name O'Niel, this will break the internal query, as the ' will be parsed as the end of the search term and will result in an error since what follows will almost certainly not be valid expression syntax. Fortunately, escaping quotes is easy and you don't need to worry about using the ` character to escape.

Consider the following example, where we want to find all users whose name field contains the O' string, like with the O'Niel example above. In this example, the search term is provided with a variable, like in the OP's use case. Simply use double-quotes for the -Filter string, and provide "" for the internal quotes as well (this is rendered identically to `"):

$term = "O'Niel"
Get-ADUser -Filter "Name -like ""*$term*"""

This doesn't break the internal query because now " signify the term boundary instead of '.

Note: If your search term contains ", you will need to escape instead for that. If your search term contains both ' and ", you can't use -Filter to search for that specific term. However, you can utilize -LDAPFilter for this since quotes are not used as the search term bounds.

Things get a bit more complicated when you are searching fields that return a DistinguishedName, but I won't clutter this answer with those details. Read the above link for more information, I will eventually be updating my linked answer at the bottom with more information about escape sequences and DN/CN filtering.


See this answer I wrote for more information about using the -Filter parameter effectively with the RSAT AD cmdlets. I have updated that answer with more complete information on filter escapes and handling the "names with quotes" cases.

  • Related