Need a powershell script to export email address of users from a list of samaccountnames

I've a CSV file with samaccountnames in it. can someone help with a powershell script to get the email address from the CSV file?.
P SAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
P SConnect With a Mentor Author Commented:
I figured it out. Just made few modifications to your script and it worked.

Import-csv C:\temp\Input.csv | %{ Get-aduser $_.samaccountname -Properties *} | select samaccountname,mail | export-csv C:\temp\InputUser.csv

Thanks again!!!
0
 
SubsunConnect With a Mentor Commented:
Try..
Import-csv C:\input.csv | %{ Get-Mailbox $_.samaccountname | Select DisplayName,PrimarySmtpAddress, @{N="EmailAddresses";E={$_.EmailAddresses |? {$_.PrefixString -ceq "smtp"} | %{$_.SmtpAddress}}} }

Open in new window


C:\input.csv format..
samaccountname 
UserA
UserB
UserC

Open in new window

To export report to csv..
Import-csv C:\input.csv | %{ Get-Mailbox $_.samaccountname | Select DisplayName,PrimarySmtpAddress, @{N="EmailAddresses";E={$_.EmailAddresses |? {$_.PrefixString -ceq "smtp"} | %{$_.SmtpAddress}}} } | Export-csv C:\report.csv -nti

Open in new window

0
 
P SAuthor Commented:
Hey Subsun, Thanks for the response but i am getting error messages while running the script. i've a CSV file with samaccountnames like below.

samaccountname
Test1
Test2
Test3

I just need to retrieve email address of each and every username.

Thanks again!!!
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
SubsunCommented:
Can you post the Error?
0
 
SubsunCommented:
I don't see any issue, the script should work if you are running the code from Exchange Management Shell.
0
 
P SAuthor Commented:
Sorry but i wasn' t using Exchange shell. It's AD module Powershell.
0
 
SubsunCommented:
Ok.. Then Get-Mailbox wont work, you can use Get-ADuser as mentioned in your comment..  :-)
0
 
Todd NelsonConnect With a Mentor Systems EngineerCommented:
You might need the proxy addresses too...

Import-Csv "C:\Temp\Input.csv" | ForEach-Object { Get-ADUser -Identity $_.samaccountname -Properties * | Select-Object Name,SamAccountName,EmailAddress,@{Name="proxyAddresses";Expression={$_.proxyAddresses}} } | Export-Csv "C:\temp\InputUser.csv" -NoTypeInformation

Open in new window

0
 
SubsunCommented:
Question can be closed if @A D doesn't have any further questions..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.