using get-aduser command in Powershell

Welcome Forums General PowerShell Q&A using get-aduser command in Powershell

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

7 months ago.

  • Author
  • #112589

    Topics: 1
    Replies: 1
    Points: 1
    Rank: Member

    Hi there,

    I need some help on how to pass a powershell variable to get-aduser account. For example,

    I can run this successfully:

    get-aduser -filter {name -like "*smith*"} | select SamAccountName

    but if I am reading a bunch of names for a file, then I need to be able to pass the names as a variable, like this as an example:


    get-aduser -filter {name -like "*$username*"} | select SamAccountName

    doesn't work, I think the variable doesn't get resolved. Any ideas how to fix it. Thank you.




  • #112600

    Topics: 0
    Replies: 11
    Points: 103
    Team Member
    Rank: Participant

    There are different ways to write a filter for AD
    Try like so:

    get-aduser -filter "name -like '*$username*'" | select SamAccountName
    • #112606

      Topics: 1
      Replies: 1
      Points: 1
      Rank: Member

      Awesome, thank you Alex,

      I tried:

      'name -like "*$username*"'

      but not

      "name -like '*$username*'"

      I thought single comma in front of a variable causes it not to resolve, but I guess I was wrong.

      Thanks again


    • #112663

      Topics: 8
      Replies: 736
      Points: 2,059
      Helping Hand
      Rank: Community Hero

      You are correct as well as wrong.

      Single quote will not resolve variables, but double quotes do, that means if the single quotes are inside double quotes, it will resolve.

      See below on quoting rules, I suggest you to read this document.

The topic ‘using get-aduser command in Powershell’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort