question

RogerRoger-2394 avatar image
0 Votes"
RogerRoger-2394 asked ChristianGlck-4103 commented

Export AD UPN and SamAccount Name

Hi All

i have email addresses in csv file, my csv file is in below format. How can i export samaccountname, UPN by importing the email addresses.

user
user1@contoso.com
user2@contoso.com

When i try the below syntax i am getting error

foreach($user in (get-content C:\temp\input.csv){
get-aduser -filter {emailaddress -eq $user} |select -property displayname, samaccountname,UserPrincipalName, emailaddress |
Export-Csv C:\temp\output.csv -NoTypeInformation
}

windows-server-powershelloffice-exchange-server-administrationwindows-active-directoryoffice-exchange-online-itpro
5 |1600 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.

1 Answer

IanXue-MSFT avatar image
1 Vote"
IanXue-MSFT answered ChristianGlck-4103 commented

Hi,

Please check to see if this works.

 $input = "C:\temp\input.csv"
 $output = "C:\temp\output.csv "
 Import-Csv -Path $input | ForEach-Object {
     $mail = $_.user
     Get-ADUser -Filter {EmailAddress -eq $mail} -Properties DisplayName, EmailAddress | 
         Select-Object -Property DisplayName, SamAccountName,UserPrincipalName, EmailAddress
 } | Export-Csv -Path $output -NoTypeInformation

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.

· 1
5 |1600 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.

Hi,

Thanks for the script, i noticed one error, though.
The variable $input is not writable for me, so i had to replace it with something like $csv.
Then it's working.

Kind regards,
Chris

0 Votes 0 ·