extract SamAccountName from email address csv file

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?
Who is Participating?
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
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

Gopi RajuSystem AnalystCommented:
Get-ADUser -Filter "Name -eq '$user'" | Select-Object name, samaccountname
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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:
"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:

$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
"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.
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

dougdogAuthor Commented:

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
dougdogAuthor 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 / EngineerCommented:
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 Network EngineerCommented:
Here this does the deed from scratch:

#Import-Module activedirectory
Import-Module activedirectory

# Put the path you want the CSV File to be in

# 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 ArchitectCommented:
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:

-- 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.

Experts-Exchange Cleanup Volunteer
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.