Bobazonski
Bobazonski

Reputation: 1565

Powershell -like won't compare to variable

In Powershell, I can do this:

    $Useraccount = Get-ADUser -Filter { Name -like "*Smith*"}

and find some user(s), but when I do this:

    $namefilter = "Smith"
    $Useraccount = Get-ADUser -Filter { Name -like "*$namefilter*"}

nothing is found. Why?

Upvotes: 0

Views: 879

Answers (1)

Bacon Bits
Bacon Bits

Reputation: 32170

-Filter looks like it accepts a script block like Where-Object, but it's actually a string. If you use the curly braces syntax, it tends to treat it like a literal string, so variables won't expand.

Try:

$Useraccount = Get-ADUser -Filter "Name -like '*$namefilter*'"

Upvotes: 3

Related Questions