Using -filter with variables in string

Welcome Forums General PowerShell Q&A Using -filter with variables in string

This topic contains 24 replies, has 6 voices, and was last updated by

 
Participant
2 months ago.

  • Author
    Posts
  • #166283

    Participant
    Topics: 13
    Replies: 42
    Points: 243
    Rank: Participant

    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.

  • #166301

    Participant
    Topics: 20
    Replies: 32
    Points: 160
    Helping Hand
    Rank: Participant

    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.

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

    HTH

    Roy.

    • #166313

      Participant
      Topics: 1
      Replies: 59
      Points: 328
      Helping Hand
      Rank: Contributor

      Quoting, my old friend....we meet again.

      This will work:

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

    Participant
    Topics: 13
    Replies: 42
    Points: 243
    Rank: Participant

    Quoting, my old friend....we meet again.

    This will work:

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

    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

    • #166657

      Participant
      Topics: 1
      Replies: 59
      Points: 328
      Helping Hand
      Rank: Contributor

      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.

  • #166651
    Naw

    Participant
    Topics: 4
    Replies: 42
    Points: 74
    Rank: Member

    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.

  • #166696

    Participant
    Topics: 13
    Replies: 42
    Points: 243
    Rank: Participant

    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?

    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.

    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}
  • #166708
    js

    Participant
    Topics: 25
    Replies: 678
    Points: 1,629
    Helping Hand
    Rank: Community Hero

    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.

  • #166711

    Participant
    Topics: 13
    Replies: 42
    Points: 243
    Rank: Participant

    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.

    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.

    • #166714
      js

      Participant
      Topics: 25
      Replies: 678
      Points: 1,629
      Helping Hand
      Rank: Community Hero
      PS C:\users\admin> $a = 'hi'
      PS C:\users\admin> {$a}.tostring()
      $a
      PS C:\users\admin> "$a"
      hi
      
    • #166732

      Participant
      Topics: 13
      Replies: 42
      Points: 243
      Rank: Participant
      PowerShell
      6 lines

      1
      2
      3
      4
      5
      6
      PS C:\users\admin> $a = 'hi'
      PS C:\users\admin> {$a}.tostring()
      $a
      PS C:\users\admin> "$a"
      hi
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      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?

    • #166735
      js

      Participant
      Topics: 25
      Replies: 678
      Points: 1,629
      Helping Hand
      Rank: Community Hero

      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*"
      
  • #166738

    Participant
    Topics: 13
    Replies: 42
    Points: 243
    Rank: Participant

    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

    1
    2
    3
    4
    'hi' -like $a
    'hi' -like "*$as*"
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    I appreciate you sticking with the thread. Hopefully someone here understands the oddities of powershell lol

    • #166741
      js

      Participant
      Topics: 25
      Replies: 678
      Points: 1,629
      Helping Hand
      Rank: Community Hero

      What's that box with the 1 in it?

    • #166933

      Participant
      Topics: 1
      Replies: 59
      Points: 328
      Helping Hand
      Rank: Contributor

      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.

  • #166744

    Participant
    Topics: 13
    Replies: 42
    Points: 243
    Rank: Participant

    What's that box with the 1 in it?

    No idea, it looks like my quoting hasn't been working correctly. It should have quoted your 4 lines of code

  • #166945
    js

    Participant
    Topics: 25
    Replies: 678
    Points: 1,629
    Helping Hand
    Rank: Community Hero

    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
    
  • #166966

    Participant
    Topics: 13
    Replies: 42
    Points: 243
    Rank: Participant

    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

    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
  • #167134

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    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...

  • #167209

    Participant
    Topics: 13
    Replies: 42
    Points: 243
    Rank: Participant

    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...

    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*"}
  • #167221

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    you would do it like below

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

    Participant
    Topics: 13
    Replies: 42
    Points: 243
    Rank: Participant

    you would do it like below

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

    Is there a reason you used single quotes instead of double quotes?

    E.g.

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

    I get the same result, but I suppose the single quotes way is cleaner.

  • #167233

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    Is there a reason you used single quotes instead of double quotes?

    unless escaped or using here strings, same type of quotes cannot be used with in them.

  • #167236
    js

    Participant
    Topics: 25
    Replies: 678
    Points: 1,629
    Helping Hand
    Rank: Community Hero

    The filter isn't really a script block, but it seems like everybody uses it, even the online help.

  • #167269

    Participant
    Topics: 13
    Replies: 42
    Points: 243
    Rank: Participant

    Thank you both for the information!

You must be logged in to reply to this topic.