Solved

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

Posted on 2016-10-19
9
41 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

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 15

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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 …

752 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