pulling user account info with powershell

I have a csv file with two columms of first names and lastname . I need to use a powershell to to go thru AD and pull into an exported csv file the results for each user  = samaaccount name, email , and display name. also there is one unique thing here , in order to get the correct name a match by last name needs to occur because I don't need to get all the , for example  smiths in the org , if the first name is joe smith in the csv I need to get joe smith and not Josephine smith
gd6627Asked:
Who is Participating?
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
np just select the helpful answers and the final answers so it can be considered ur question closed.
0
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
I don't really understand your question
you have a csv with (name, lastname).

then you want to pull an exported csv file "the results of each user".
the result of what?

If you want to pull name and lastname in the first csv file, you need a 3rd column to be the matching parameter of something that you already have in the AD, like samaccountname.

with this 3 columns (name,lastname,samaccountname) you will be able to match the samaccountname to add name, lastname and displayname.
So you can get the samaccountname, email, displayname exported (to csv) from there.

jose
1
 
gd6627Author Commented:
Ok let me see if I can explain it better I have a CVS file with first names and last names that I need to run thru powershell to check ad for them , then once it finds the match user by first name and last name give me the results of those users into another CVS file that will show the users display name samaccoutname and email
0
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

 
yo_beeDirector of Information TechnologyCommented:
So  you have a CSV with
LastName, FirstName
Smith, Joe
Smith, Josephine  

Will the SN and GivenName match Joe Smith or Josephine  Smith?
0
 
gd6627Author Commented:
Can we do like a match variable > where the last name on the csv matches last name on AD so in your example when the script goes thru it will spit out both but because the samaccount is different it will show two diferent logons  for samaccount

Joe Smith  samaaccount josmith

Josephine Smith : samaccount jossmith
0
 
yo_beeDirector of Information TechnologyCommented:
Yes, but what if someone has the same lastname?
0
 
gd6627Author Commented:
I don't think it matters because your quering Ad for the names on the imported csv and in the select your asking AD to show you the first , last name and samaccount of the matched results I am assuming. I think its ok to have same last names lots of people do but in ad samaccounts are unique .

Am just not sure how to build the script with the AD cmdlts I seen something like this done with Quest cmdlts but I cant use quest
0
 
FOXActive Directory/Exchange EngineerCommented:
gd6627- As Jose mentioned earlier you need to have something already from AD which you want to use in your .csv.   If you narrow it down to the samaccountnames you will not have to worry about the same last names, etc.  Create a .csv file (Users.csv) with the header Samaccountname

script:
Import-Csv "c:\temp\Users.csv" | %{Get-ADUser $_.Samaccountname  -properties * | Select samaccountname,  emailaddress, DisplayName | sort DisplayName | Export-csv "C:\temp\results.csv" -notype}
0
 
yo_beeDirector of Information TechnologyCommented:
Can you give an sample CSV?
From what you are explaining you are taking a csv and want to match it to an AD user?
Am I reading this incorrectly?
0
 
FOXActive Directory/Exchange EngineerCommented:
gd6627- As Jose mentioned earlier you need to have something already from AD which you want to use in your .csv.   If you narrow it down to the samaccountnames you will not have to worry about the same last names, etc.  Create a .csv file (Users.csv) with the header Samaccountname

script: correction
Import-Csv "c:\temp\Users.csv" | %{Get-ADUser $_.Samaccountname  -properties * | Select samaccountname,  emailaddress, Surname, GivenName, DisplayName | sort Surname} | Export-csv "C:\temp\results.csv" -notype}
0
 
gd6627Author Commented:
YEs heres a sample csv that just has the headers as Fn and LN for first name and last name

Heres a QUEst code that does the look up I just need something like this but with AD Cmdlts not QUEST

$users='c:\Testnames.csv'

Import-Csv $users | foreach {
Get-QADUser -FirstName $_.fn -LastName $_.ln | Where {$_.ln -match $_.ln } | select firstname,lastname,samaccountname } `
| Export-Csv
TEstNAme.csv
0
 
yo_beeDirector of Information TechnologyCommented:
@gd6627

What is someone has the same last name?
0
 
yo_beeDirector of Information TechnologyCommented:
Also you script does not make much sense.
You are creating an array from a CSV then you want to use a value from that array to get an AD user and you want to filter with the same value that you used to get the user.
0
 
gd6627Author Commented:
Ok i didnt write it thats how the user before was doing it
0
 
yo_beeDirector of Information TechnologyCommented:
Your goal ultimately is what?

The whole csv as a reference does not seem to make any sense for this.
0
 
gd6627Author Commented:
what is does is it looks in AD for that name and matches the last names if theres a match and then it spits out the name and samaccount from AD
0
 
gd6627Author Commented:
This is what am trying to accomplish

 import a csv file with first names and lastnames into powershell and then have it search AD to see if theres a match then give me the info in the select statement from that user ?

So if the import CSV file has
Joe Smith

The script does a search if it doesn't find joe smith it moves on .

if it does then

Select  select firstname,lastname,samaccountname  or what ever I place after the select statement fro the users properties
0
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
Import-csv "C:\Users\jortega\Desktop\fnfn.csv" | foreach{
    $UserName=$_.fn + "*"
    $lastName=$_.ln + "*"
    $user =Get-AdUser -filter{Name -like $UserName -and SurName -like $lastName} -Properties * | select * 
    
    if($user){
        #all properties and fields are already in the $user if found, just need to filter whichever you need in here (or you can modify the $user 's variable select statement)
        $user | select Name,Surname,SamaccountName
    }
}

Open in new window


Don't use quest, all that quest did, now powershell does it.
0
 
gd6627Author Commented:
Thank you Jose
1
 
gd6627Author Commented:
Thank you for your help
0
 
yo_beeDirector of Information TechnologyCommented:
I think the proper answer should be accepted.
Jose gave many solutions, but the one you accepted does not give anyone insight.
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.