Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

powershell newbie here. I am building a simple bit of code to help me find user's by entering letters of user names. How do I get a wildcard to work w/ a variable? this works:

$name=read-host -prompt "enter user's first or last initial"
$userInput=get-aduser -f {givenname -like 'A*' } 
cmd /c echo "output: $userInput"

this does not:

$name=read-host -prompt "enter user's first or last initial"
$userInput=get-aduser -f {givenname -like '$name*' } 
cmd /c echo "output: $userInput"

The first bit of code delivers a list of users with "A" in their name. Any suggestions woudl be appreciated. thanks

share|improve this question
You can probably use Write-Host in place of cmd /c echo for the same effect. – jscott Jun 26 '13 at 0:23
$lastname = "smith"
Get-ADUser -LDAPFilter "(&(objectCategory=person)(objectClass=user)(Sn=*$lastname*))"

Returns a list of users whose last name (surname) matches the search pattern of *smith*, so it will return Haysmith, Smithwood, McSmithers, etc.

share|improve this answer
Thanks for the input but I am getting this error with that code: Get-ADUser : The search filter cannot be recognized At line:1 char:1 + Get-ADUser -LDAPFilter "(&(objectCategory=person)(objectClass=user)(Sn=*$lastnam ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException + FullyQualifiedErrorId : The search filter cannot be recognized,Microsoft.ActiveDirectory.Management.Commands.GetADUser – user179037 Jun 25 '13 at 12:29
That will happen if the variable has not been created and assigned a value. I.E. you do not have $lastname="smith" above the call. – Pablo Jun 25 '13 at 13:02
ah yes of course...a newbie I am. That worked thanks. Now to work on a UI, formatting and other things. – user179037 Jun 25 '13 at 13:13

When single quotation marks are used, escape characters and automatic string replacement (e.g. using a variable inside the string) are eliminated.

While Ryan Ries answer will work with the -LDAPFilter parameter, to work with the -Filter parameter you will have to concatenate or use escape characters.

Using escape characters (An apostrophe before the character):

$userInput=get-aduser -f "GivenName -like `"$name*`""

Using string concatenation:

$userInput=get-aduser -f ('GivenName -like "' + $name + '*"')

Better readability:

$name = read-host -prompt "enter user's first or last initial"
$filter = 'GivenName -like "' + $name + '*"'
$userInput = Get-ADUser -Filter $filter
share|improve this answer
thanks, I was wondering how it would play out with -filter – user179037 Jun 28 '13 at 20:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.