Using -filter with variables in string

This is something I wrote to retrieve a user object based on their first and last name, as we have it stored in AD.

$firstName = "My"
$lastName = "Name"

Get-ADUser -filter {DisplayName -like "*$lastName*$firstName*"}

This doesn’t work though, but this does work:

$firstName = "My"
$lastName = "Name"

$name = "*$lastName*$firstName*"

Get-ADUser -filter {DisplayName -like $name}

What’s wrong with my original code?

I did a test and printed the string to console to make sure it was what I thought it was, and indeed it was.

I also tried doing this instead:

 {DisplayName -like "*$($lastName)*$($firstName)*"}

Same result though.

Don’t like the way you doing it. However, I m getting output as per your code. Might be the user you are trying to filter might be residing at another domain. Try to use server parameter.

[pre]

Get-aduser -filter {Displayname -like $name} -server us.posh.org

[/pre]

HTH

Roy.

Quoting, my old friend…we meet again.

This will work:

Get-ADUser -filter "DisplayName -like '*$lastName*$firstName*'"

[quote quote=166313]Quoting, my old friend…we meet again.

This will work:<textarea class=“ace_text-input” style=“opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=“false” wrap=“off”></textarea>

Get-ADUser -filter "DisplayName -like '*$lastName*$firstName*'"
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] That's ridiculous. Somehow that works but if I instead use a bracket, it doesn't...

E.g.

Get-ADUser -filter {DisplayName -like '*$lastName*$firstName*'}

This doesn’t work, but this works fine:

Get-ADUser -filter "DisplayName -like '*$lastName*$firstName*'"

You can’t tell me that Powershell is 100% functional lol

You can't tell me that Powershell is 100% functional lol
I came across this myself too. And I said to myself {} represents code block. "" represents a bunch of string.

About a year ago I wrote a complex/convoluted script to go through AD and create list of users from groups. I fought the filter command for AGES, I even went as far as doing invoke-expression, and that worked. Someone on here pointed out that you didn’t need the {} around the filter and then it worked.

 

I used a different user name at the time, and I have NO idea what it was.

[quote quote=166657][/quote]
The weird thing is that I’ve seen so many examples of it being used with the {} and it’s worked just fine.

I can do a filter like {passwordneverexpires -eq “False”} and that works perfect. I don’t see the difference. Why would that work but not work for my previous command?

So are you saying that since DisplayName is a string, I should use the quotes (“”) instead?

That doesn’t explain why this works, since it has {} instead

Get-ADUser -filter {DisplayName -like $name}

It’s a quoting issue. Technically, an AD filter is a string. The Backus-Naur form in the docs is incorrect. The script block gets converted to a string easily, so people often use it. A variable needs to be inside double quotes to get interpreted.

[quote quote=166708]It’s a quoting issue. Technically, an ad filter is a string. The script block gets converted to a string easily, so people often use it.

[/quote]
In simple terms, where exactly is my thinking going off the rails here?

I can’t understand why this works:

Get-ADUser -filter {DisplayName -like $name}

But this doesn’t:

Get-ADUser -filter {DisplayName -like "*$lastName*$firstName*"}

They are both strings.

PS C:\users\admin> $a = 'hi'
PS C:\users\admin> {$a}.tostring()
$a
PS C:\users\admin> "$a"
hi

That clears up the difference between the brackets {} and the quotes “”. However, “$lastName$firstName*” and $name are both strings, so why would using $name work?

I think because in the first case, you don’t need to pass in a second level of quotes, but in the second case you do. Doing -like with no wildcards is really the same as -eq. Actually, I don’t know why the first case works, then.

'hi' -like $a

'hi' -like "*$as*"

[quote quote=166735]I think because in the first case, you don’t need to pass in a second level of quotes, but in the second case you do. Doing -like with no wildcards is really the same as -eq. Actually, I don’t know why the first case works, then.

PowerShell
4 lines
<textarea class="ace_text-input" style="opacity: 0; height: 17.6px; width: 6.59775px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
'hi' -like $a
'hi' -like "*$as*"
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] I appreciate you sticking with the thread. Hopefully someone here understands the oddities of powershell lol

What’s that box with the 1 in it?

[quote quote=166741]What’s that box with the 1 in it?

[/quote]
No idea, it looks like my quoting hasn’t been working correctly. It should have quoted your 4 lines of code

There are times when I find powershell to be more of an art than a science. Sometimes things work when they shouldn’t, other times they don’t when they should.

I can tell you when it comes to nest " ’ and `’ " inside of anything, I start to shudder.

Hmm, I wonder how it works?

function runfilter {
  param([string]$filter)

  echo hi | where -filterscript $filter
}

PS C:\users\j> runfilter {$_ -eq "hi"}
Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert the "$_ -eq "hi"" value of
type "System.String" to type "System.Management.Automation.ScriptBlock".
At C:\Users\j\runfilter.ps1:3 char:31
+ echo hi | where -filterscript $filter
+                               ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WhereOb
   jectCommand

[quote quote=166945]Hmm, I wonder how it works?

function runfilter {

param([string]$filter)
echo hi | where -filterscript $filter
}
PS C:\users\j> runfilter {$_ -eq "hi"}
Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert the "$_ -eq "hi"" value of
type "System.String" to type "System.Management.Automation.ScriptBlock".
At C:\Users\j\runfilter.ps1:3 char:31
+ echo hi | where -filterscript $filter
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WhereOb
jectCommand
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] No idea, hopefully someone else will chime in here. This thing is killing me. I think Powershell has bugs that haven't been fixed.
$firstName = "My"
$lastName = "Name"

#$name = “$lastName$firstName*”

#Get-ADUser -filter {DisplayName -like $name} #This works
Get-ADUser -filter {DisplayName -like “$lastName$firstName*”} #This doesn’t work

its the quotes…

$Name = "*$lastName*$firstName*"

# This works
Get-ADUser -Filter {DisplayName -like $Name}

# This doesn't
Get-ADUser -Filter {DisplayName -like "$Name"}

Variable expansion happens inside the filter but not the string expansion…

[quote quote=167134]its the quotes…

$Name = "*$lastName*$firstName*"
# This works
Get-ADUser -Filter {DisplayName -like $Name}
# This doesn't
Get-ADUser -Filter {DisplayName -like "$Name"}
Variable expansion happens inside the filter but not the string expansion...

[/quote]
How would you get it to work without using the extra variable ($name)?
For instance, how do you convert this to make it work?

$firstName = "My"
$lastName = "Name"
Get-ADUser -filter {DisplayName -like "*$lastName*$firstName*"}