question

JackyGuo-9227 avatar image
0 Votes"
JackyGuo-9227 asked ·

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

I just can find one by one

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

windows-server-powershell
· 3
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What are the names of the AD users you want to get? Your command only gets the users with the names containing “displayname”.

0 Votes 0 ·

I need to query the information of more than 200 people only have there displayname. i mean the displayname is a field of AD.

0 Votes 0 ·

BTW Thanks for your reply

0 Votes 0 ·

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi,

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.





· 8 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so much! BTW,If I create a CSV file of all the displayname, how to do the loop from the CSV file.

0 Votes 0 ·

Try this (assuming the CSV has a column named "displayname"):

 foreach($displayname in (Import-CSV x:\file.csv).displayname)
 {
      Get-ADUser -Filter {displayname -eq $_} -Properties EmailAddress | Format-table Name,SamAccountName,EmailAddress -AutoSize
 }
0 Votes 0 ·
JackyGuo-9227 avatar image JackyGuo-9227 RichMatheisen-8856 ·

I do not know why it is error.


 Get-ADUser : Variable: '_' found in expression: $_ is not defined.
 At line:3 char:7
 +       Get-ADUser -Filter {displayname -eq $_} -Properties EmailAddres ...
 +       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
     + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Comm
    ands.GetADUser
0 Votes 0 ·
Show more comments