How to output many people's SamAccountName in batch by powershell

Jacky Guo 21 Reputation points

I just can find one by one

Get-aduser -filter 'Name -like "displayname" ' -Properties * | Format-table Name,SamAccountName,EmailAddress -A

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,455 questions
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 34,191 Reputation points Microsoft Vendor


    Suppose you have an array of displaynames $displaynames, then you can do something like the following

    $displaynames = ’dn1‘,'dn2','dn3'  
    foreach($displayname in $displaynames)  
        Get-ADUser -Filter {displayname -eq $displayname} -Properties EmailAddress | Format-table Name,SamAccountName,EmailAddress -AutoSize  

    Best Regards,
    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful