active directory

I am looking for a script to run on my DC to retrieve  users their email address which is of their company like

abc@dunhill.com, right now I excel sheet they have different email address like xyz.com

I am attaching the excel sheet where for ex. I have put 2 usernames, how should I use them in script

 is there any script I can use

where changes should I make in this excel sheet so I can display both users email address

Get-Aduser -filter * -properties * | select DisplayName, EmailAddress | Sort DisplayName | out-file 'c:\temp\emailaddresses.csv'
OR
Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "DC=na,DC=XX,DC=com" |            
  Set-ADUser -replace @{employeeNumber = $($user.employeeNumber)}        
}

Can we modify the script above
Copy-of-List.xlsx
pramod1Asked:
Who is Participating?
 
PberConnect With a Mentor Solutions ArchitectCommented:
Same script, outputs only displayname and emailaddress:

$users = Import-Csv -Path C:\temp\users.csv      

foreach ($user in $users) {            
  Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties DisplayName, EmailAddress | Select-Object DisplayName, EmailAddress | Sort DisplayName | Export-Csv 'c:\temp\emailaddresses.csv'
 }

Open in new window

0
 
PberSolutions ArchitectCommented:
It is unclear what you are trying to do.  Are you trying to export to an excel list?

Your script here is good.  I would change out-file to Export-Csv:

Get-Aduser -filter * -properties * | select DisplayName, EmailAddress | Sort DisplayName | Export-Csv 'c:\temp\emailaddresses.csv'

Open in new window

0
 
PberSolutions ArchitectCommented:
Are you trying to set employeeNumber?
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
pramod1Author Commented:
I have got a excel sheet having users name and their email address which may be gmail.com or some other email address, all I need to do is run script and get their corporate email address  which is for ex. abc@dunhill.com right now in excel sheet each users are having different email address their secondary email address, I need to get only dunhill.com
0
 
pramod1Author Commented:
no I am not setting employe number.

I got this request

list of xyz domain users email address and requestor is looking for their xxx domain email address
0
 
pramod1Author Commented:
what should I put in csv file as I have list of 600 users
0
 
PberSolutions ArchitectCommented:
So when you run the script above, you may get various email addresses as opposed to just the ones for dunhill.com
0
 
pramod1Author Commented:
in excel sheet which I got have their names and email address
0
 
pramod1Author Commented:
can you share my screen, I have excel sheet with usernames and each user having diff. email address but I need to get one email address for all those 600 users
for ex. dunhill.com
0
 
PberSolutions ArchitectCommented:
Still unclear, so do you want to run a query against the excel spreadsheet to get only specific email addresses
0
 
pramod1Author Commented:
yes
0
 
PberSolutions ArchitectCommented:
You can do that right from excel, or do you really need it in powershell?
0
 
pramod1Author Commented:
is there any number I can call, I have 600 users list which are having their previous domain name email address

like abc@ge.com now they migrated to synchrony so the requester is asking me to get all 600 users synchrony email address

so what should I put in csv file their name and email address provided and what script should I run
0
 
PberSolutions ArchitectCommented:
In excel do this.  Click the Data Tab, then click the Filter icon in the ribbon bar.  In the spreadsheet, click the drop down that is now showing up for the e-Mail, then in the search box, put @xyz.com
2017-12-05-14_23_32-Get-aduser--Filt.jpg
0
 
pramod1Author Commented:
none show synchrony in excel sheet , I have to run on AD to get their email address
0
 
PberSolutions ArchitectCommented:
What are you planning on doing with the spreadsheet.  Do you need to change the email attribute in ad to the new email address, or are you just providing this spreadsheet to someone
0
 
pramod1Author Commented:
do you have any number I can call you. I have got excel sheet having usernames and their previous email address

I need to get details of 600 users in excel sheet their new email address in their new company which they migrated
0
 
PberSolutions ArchitectCommented:
Still not perfectly clear what you want.  From what I understand, you have a spreadsheet with 600 usernames and email addresses.  The problem is the email addresses are incorrect.  So do you just need to modify the spreadsheet to edit the email addresses to the new email address, or do you need to do stuff in AD?
0
 
PberSolutions ArchitectCommented:
BTW:  no I don't have a number to call.  Against EE policy
0
 
pramod1Author Commented:
"From what I understand, you have a spreadsheet with 600 usernames and email addresses.  The problem is the email addresses are incorrect" - till here u are correct either these users are having abc@ge.com(previous company) or Hotmail.com or gamil.com

I need their email address in present company as all are migrated and are having abc@dunhill.com which I want to make sure
0
 
pramod1Author Commented:
so I need to run script in AD based upon their sam account name and get their new email address
0
 
PberSolutions ArchitectCommented:
Oh, hang on.
0
 
pramod1Author Commented:
so while preparing the csv file should I include their username and old email address or just username
0
 
PberSolutions ArchitectCommented:
Is the samaccountname in the spreadsheet to look up?
0
 
pramod1Author Commented:
yes their anmes are given in excel sheet which is perhaps their sam account name
0
 
pramod1Author Commented:
names sorry typo
0
 
PberSolutions ArchitectCommented:
The excel sample appears to contain their full name.  Did you get that list from this script:

Get-Aduser -filter * -properties * | select DisplayName, EmailAddress | Sort DisplayName | out-file 'c:\temp\emailaddresses.csv'

Open in new window

0
 
pramod1Author Commented:
no, if I run this script , how I am making sure that this script is running against those 600 users
0
 
PberSolutions ArchitectCommented:
The reason I ask is that full names isn't the best "Key" to look for users, usually one uses samAcountName.
0
 
pramod1Author Commented:
so what should I put in csv file so the script pulls
0
 
pramod1Author Commented:
where is this command pulling up:Get-Aduser -filter * -properties * | where should I put csv file having 600 users
0
 
PberSolutions ArchitectCommented:
Would this work:

$users = Import-Csv -Path C:\temp\users.csv      

foreach ($user in $users) {            
  Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties DisplayName, EmailAddress | Sort DisplayName | Export-Csv 'c:\temp\emailaddresses.csv'
 }

Open in new window


This will query the file c:\temp\users.csv the samaccountname column, then lookup that user in AD and output their email address to a file c:\temp\emailaddresses.csv
0
 
pramod1Author Commented:
thanks I will try tomorrow and update you, thanks for your time
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.