Welcome Forums General PowerShell Q&A Wildcards with 'Get-ADUser -Filter' and 'variable'

Viewing 3 reply threads
  • Author
    Posts
    • #168679
      Participant
      Topics: 5
      Replies: 9
      Points: 87
      Rank: Member

      Hello,

      I have code as below:

      $person = Read-host -Prompt "Please input login or name and surname"
      
      Get-ADUser -Filter {(SamAccountName -like "*$person*") -OR (Name -like "*$person*")}

      Without wildcards (asterix) looks, like it works correctly, but when I try to add asterix and quotes this code show nothing.

      Does someone know why is that?

      Best Regards

      Daniel

       

    • #168715
      js
      Participant
      Topics: 28
      Replies: 749
      Points: 2,082
      Helping Hand
      Rank: Community Hero

      This is a frequently asked question. A filter parameter is actually a string. You can form it this way. -Or has lower precedence than -like, so you don’t need the parentheses:

      Get-ADUser -Filter "SamAccountName -like '*$person*' -OR Name -like '*$person*'"
      

      I don’t pretend to understand the black magic when a script block is converted into a string and then passed into get-aduser.

    • #168745
      Participant
      Topics: 5
      Replies: 9
      Points: 87
      Rank: Member

      It works 🙂

      But this is really black magic….

    • #168751
      js
      Participant
      Topics: 28
      Replies: 749
      Points: 2,082
      Helping Hand
      Rank: Community Hero

      Yeah with these filters it’s more like “see if it works this way…”

Viewing 3 reply threads
  • The topic ‘Wildcards with 'Get-ADUser -Filter' and 'variable'’ is closed to new replies.