Wildcards with 'Get-ADUser -Filter' and 'variable'

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

This topic contains 3 replies, has 2 voices, and was last updated by

js
 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #168679

    Participant
    Topics: 3
    Replies: 7
    Points: 56
    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: 24
    Replies: 677
    Points: 1,613
    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: 3
    Replies: 7
    Points: 56
    Rank: Member

    It works 🙂

    But this is really black magic....

  • #168751
    js

    Participant
    Topics: 24
    Replies: 677
    Points: 1,613
    Helping Hand
    Rank: Community Hero

    Yeah with these filters it's more like "see if it works this way..."

You must be logged in to reply to this topic.