Solved

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

Posted on 2016-10-19
9
25 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)
Comment Utility
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
Comment Utility
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
Comment Utility
Can you post the Error?
0
 

Accepted Solution

by:
A D earned 250 total points (awarded by participants)
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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
Comment Utility
Sorry but i wasn' t using Exchange shell. It's AD module Powershell.
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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)
Comment Utility
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
Comment Utility
Question can be closed if @A D doesn't have any further questions..
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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 …

772 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

9 Experts available now in Live!

Get 1:1 Help Now