extract SamAccountName from email address csv file

dougdog
dougdog used Ask the Experts™
on
i have a csv file populated with email addresses
i also need there SamAccountName
Rather than look each of these up
Is there a method that could query the email addresses in the csv and populate another column with the SamAccountName?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Gopi RajuSystem Engineer

Commented:
Get-ADUser -Filter "Name -eq '$user'" | Select-Object name, samaccountname
Senior Network Consultant / Engineer
Commented:
You could use something like this:

$CSVout = "C:\Userlist.csv
Get-ADUser -Filter * -Properties EmailAddress | Select SamAccountName,EmailAddress | Export-CSV $CSVout -notype 

Open in new window

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
May I assume the users emails are found in your AD?

If so then you can amend your CSV using the code below.

Example Starting CSV:
"email","Column2"
"User@YourDomain.com","Column2 Value"
"User2@YourDomain.com","Another Column2 Value"

Open in new window


Powershell code to replace the original CSV with one which contains the column of values:

$CSVFile="\\tsclient\C\Admin\Experts-Exchange\29066762\emails2.csv"
$Obj = Import-CSV $CSVFile
$NewObj = @()
Foreach ($O in $Obj) {
	$SAMID = Get-ADUser -properties * -filter "emailaddress -eq '$($O.email)'" | Select -expandproperty SAMAccountName
	$O | Add-Member –MemberType NoteProperty –Name SAMID –Value $SAMID
	$NewObj += $O 
}

$NewObj | Export-CSV $CSVFile

Open in new window


Example changed CSV:

#TYPE System.Management.Automation.PSCustomObject
"email","Column2","SAMID"
"User@YourDomain.com","Column2 Value","User_SAM_ID"
"User2@YourDomain.com","Another Column2 Value","User2_SAM_ID"

Open in new window


Note the above code is setup to retain ALL of the data in all of the columns you already have in your CSV.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Author

Commented:
Hi

I was unable to get it to work
is there a method to just do it from scratch?
and produce a csv file from my AD containing only user accounts with a valid email address alongside the samaccount

Author

Commented:
i did but its something im not doing correctly
am i right in saying i need to copy that code into notepad and save as .ps1
then run?
Jeremy WeisingerSenior Network Consultant / Engineer

Commented:
You can just paste it into your powershell prompt. If you're running Windows 2012 or newer you can paste as is. If it's 2008 R2, you need to first run Import-Module activedirectory
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer
Commented:
Here this does the deed from scratch:

#Import-Module activedirectory
Import-Module activedirectory

# Put the path you want the CSV File to be in
$CSVFile="C:\emails2.csv"

# Get all User Info From AD
$ADUsers = Get-ADUser -properties * -filter * | Select SAMAccountName, emailaddress

#Export to CSV:
$ADUsers | Export-CSV $CSVFile

Open in new window

PberSolutions Architect

Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Jeremy Weisinger (https:#a42356644)
-- Ben Personick (Previously QCubed) (https:#a42358360)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Pber
Experts-Exchange Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial