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

 
Participant
3 weeks, 3 days ago.

  • Author
    Posts
  • #112589

    Participant
    Points: 0
    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:

    $username="smith"

    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

    Moderator
    Points: 1
    Rank: Member

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

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

You must be logged in to reply to this topic.