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?
LVL 1
dougdogAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gopi RajuSystem EngineerCommented:
Get-ADUser -Filter "Name -eq '$user'" | Select-Object name, samaccountname
0
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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:
"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.
0
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:
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
0
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?
0
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
0
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
$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

0
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:

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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.