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

3 months, 3 weeks ago.

  • Author
  • #112589

    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

    Points: 65
    Team Member
    Rank: Member

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

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

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