Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-10-19
9
Medium Priority
?
68 Views
Last Modified: 2016-11-07
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?.
0
Comment
Question by:P S
  • 5
  • 3
9 Comments
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points (awarded by participants)
ID: 41850186
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
 

Author Comment

by:P S
ID: 41850211
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
 
LVL 40

Expert Comment

by:Subsun
ID: 41850220
Can you post the Error?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Accepted Solution

by:
P S earned 1000 total points (awarded by participants)
ID: 41850234
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
 
LVL 40

Expert Comment

by:Subsun
ID: 41850240
I don't see any issue, the script should work if you are running the code from Exchange Management Shell.
0
 

Author Comment

by:P S
ID: 41850257
Sorry but i wasn' t using Exchange shell. It's AD module Powershell.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41850359
Ok.. Then Get-Mailbox wont work, you can use Get-ADuser as mentioned in your comment..  :-)
0
 
LVL 17

Assisted Solution

by:Todd Nelson
Todd Nelson earned 500 total points (awarded by participants)
ID: 41850706
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
 
LVL 40

Expert Comment

by:Subsun
ID: 41876862
Question can be closed if @A D doesn't have any further questions..
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here's a look at newsworthy articles and community happenings during the last month.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question