Solved

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

Posted on 2016-10-19
9
28 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:A D
  • 5
  • 3
9 Comments
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 125 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:A D
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Accepted Solution

by:
A D earned 250 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:A D
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 14

Assisted Solution

by:Todd Nelson
Todd Nelson earned 125 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
A brief introduction to what I consider to be the best editor for PowerShell.
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…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now