Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

2 Answers

$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
add comment

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
add comment

Your Answer

 
discard

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.