Powershell Script to get Samaccountname & email address from list of users in CSV file.

HI Experts,

I have a CSV file of about 500 users. First Name Last Name  

1- I need to get the samaccountname (logon ID) & email address for each of these users in the file.  

Is there a nice powershell script that can accomplish this?  

Thanks
KAKA JIAsked:
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.

Sajid Shaik MSr. System AdminCommented:
install AD module for powershell then run the following script

Get-ADUser -Filter * -Properties DisplayName, EmailAddress, Title | select DisplayName, EmailAddress, Title | Export-CSV "C:\Scripts\Email_Addresses.csv"

it'll generate csv in C:\Scripts\ with name of Email_Adderesses.CSV.

all the best
0
KAKA JIAuthor Commented:
Thanks Sajid.

I also need samAccountname (logon ID)
0
Jose Gabriel Ortega CastroCEOCommented:
A oneliner would be:

Import-csv ".\csvfilewithNames.csv" | %{ $fn = $_.FirstName; $ln = $_.LastName; get-aduser -Filter{GivenName -like $fn -and SurName -like $ln} -Properties EmailAddress } | select GivenName,Surname,SamAccountName,EmailAddress

Open in new window


And to export that into Csv
Import-csv ".\csvfilewithNames.csv" |%{ $fn = $_.FirstName; $ln = $_.LastName; get-aduser -Filter{GivenName -like $fn -and SurName -like $ln} -Properties EmailAddress } | select GivenName,Surname,SamAccountName,EmailAddress | Export-Csv -NoTypeInformation .\Addedinfo.csv

Open in new window

0
KAKA JIAuthor Commented:
Thanks Jose, it seems to be working however I am not getting everyone out of 500 i only got 368. I can see the ones that are missing are enable users and have an email address.
0
Jose Gabriel Ortega CastroCEOCommented:
the condition is the "match" of the FirstName and LastName.
if you have them correctly set all, in your input file you should be getting them correctly.
if you miss a name or add more than they have in your input it won't match the like condition.

So it's not something related to the script it's related to your input file.

So try to match them just by name or last name

Just Name:
Import-csv ".\csvfilewithNames.csv" |%{ $fn = $_.FirstName; get-aduser -Filter{GivenName -like $fn} -Properties EmailAddress } | select GivenName,Surname,SamAccountName,EmailAddress | Export-Csv -NoTypeInformation .\Addedinfo.csv

Open in new window


Just LastName:
Import-csv ".\csvfilewithNames.csv" |%{ $ln = $_.LastName; get-aduser -Filter{SurName -like $ln} -Properties EmailAddress } | select GivenName,Surname,SamAccountName,EmailAddress | Export-Csv -NoTypeInformation .\Addedinfo.csv

Open in new window


Or just correct your CSV file, but I can't help with that one.
2

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