kayal24
(kayal24)
1
Hello, I have a csv file which contains GivenName and Surname column.
I would like to get their samaccountname. Anyone know how to do it?
Ive tried this but not working
Import-Csv C:\Users\Users.csv -Delimiter "," | ForEach { Get-ADUser -Filter {GivenName -like $_.GivenName -and Surname -like $_.Surname} | Select-Object -ExpandProperty sAMaccountName | Out-GridView }
4 Spice ups
Neally
(Neally)
2
Yep, a filter.
What exactly is not working ?
What format is the samaccountname?
Do the accounts have a surname and given name set?
Neally
(Neally)
3
works fine in my test:
$csv =
@"
givenname,surname
lee,gene
"@ | convertfrom-csv
$data =
foreach($row in $csv){
$sam = Get-aduser -filter "givenname -eq '$($row.givenname)' -and surname -eq '$($row.surname)'" |
select samaccountname
if($sam){$sam}
}
$data | Out-GridView -PassThru
1 Spice up
kayal24
(kayal24)
4
I tried this, but no error appears and no results…
$csv = Import-Csv -Path C:\Users\Users.csv -Encoding Default -Delimiter ";"
$data =
foreach($row in $csv){
$sam = Get-aduser -filter {(surname -eq "$($row.surname)") -and (givenname -eq "$($row.givenname)")} | Select samaccountname
if($sam){$sam}
}
$data | Export-Csv -path C:\Users\test.CSV -Encoding Default -Delimiter ";" -NoTypeInformation
Neally
(Neally)
5
how does your CSV file look like? Valid CSV format? What columns do you have?
Why do you do that delimiter stuff?
If you run it manually, does it find the account?
Get-aduser -filter {(surname -eq "ENTER_SURANME") -and (givenname -eq "ENTER_GIVENNAME")}
No result is usually when then the filter does not find anything
$csv = Import-Csv -Path C:\Users\Users.csv -Encoding Default -Delimiter ";"
$data =
foreach($row in $csv){
$sam = Get-aduser -filter {(surname -eq "$($row.surname)") -and (givenname -eq "$($row.givenname)")} | Select samaccountname
if($sam){$sam}
else{[pscustomobject]@{samaccountname = "User not found in AD - '$($row.givenname) $($row.surname)'"}}
}
$data | Export-Csv -path C:\Users\test.CSV -NoTypeInformation
kayal24
(kayal24)
6
When I do it manually it works, but in CSV format it is not able to find the samaccountname. I have a Surname column and a GivenName column. It is saved in .csv format (comma delimited csv)
Is there another way I can do this or from .txt file?
Neally
(Neally)
7
Can you post a sample of your CSV ?
rename it .txt and attach it here.
seems like it might be an issue getting the data from CSV?
kayal24
(kayal24)
8
Yes sure:
Surname;GivenName
Smith;John
Gali;Andy
Achile;Arnold
Any chance you have an extra space at the end of the names in the CSV? I see people get burned by that all the time. In which case you’re going to have to a trim in your powershell.
1 Spice up
DoctorDNS
(DoctorDNS)
10
The simplest way would be to just update the CSV to have it.
Alternatively, you can search AD for the account with a given surname and first name, and tet the SAM account from there. That being said, it might be difficult if the CSV has “John”, “Doe”, and the AD Has “J” "Doe. Yes, you can do it but there is work to do.;
What are you trying to achieve?
kayal24
(kayal24)
11
I have a list of 300 users who will need to be added to a diffusion list and unfortunately the list only contains the first and last name. Im working on it. Thanks for the help
Neally
(Neally)
12
that’s not a valid CSV format? You said it was separated by comma?
kayal24
(kayal24)
13
Yeah you are right thanks
Neally
(Neally)
14
Surname;GivenName
Gene;Lee
Gali;Andy
Achile;Arnold
$csv = import-csv "C:\Users\Administrator.AD\Desktop\file.csv" -Delimiter ";"
$data =
foreach($row in $csv){
$sam = Get-aduser -filter "givenname -eq '$($row.givenname)' -and surname -eq '$($row.surname)'"
if($sam){$sam.samaccountname}else{"Not found in AD"}
}
$data | Out-GridView -PassThru
this seems to work fine for me.
1 Spice up
kayal24
(kayal24)
15
Thanks!! it works now and there were spaces on few users in the list they gave me smh
kayal24
(kayal24)
16
Is it possible to catch the error by popping up a window message “One or more users were not found”?
Neally
(Neally)
17
Should be. What have you tried?