get-aduser samaccountname from first name and last name

I have a variable with a list of first names and a variable with a list of last names.

I need to create a get-aduser command that will compare these values and spit out the same account name and for some reason I’m having a hard time wrapping my head around this. Probably because it is early!


$FName =

$lname =

lname would be -eq and fname would be -like


What have you tried so far? Please show your code (formatted as code please) Read Me Before Posting! You’ll be Glad You Did!

Get-ADUser -Filter {(enabled -eq $True) -and (surname -eq $lname) -and (givenname -like $fname)} -Property Name,surname,givenname,SamAccountName -Server DCserver

This produces a system object failure and I went into starting to create foreach but not sure how best to handle this when I need to match both.

Please, always when you get errors you should share the complete error message as well. And when you post code or error messages or sample date or console output you should format it as code using the code tags “PRE”. Thanks in advance. Read Me Before Posting! You’ll be Glad You Did!

If I got you right you have to use a nested loop as you don’t have relations between the given names and surnames. Regardless of that you should always use a searchbase as this limits the stress you put on your DC with a query. So something like this could be a start for you:

$Searchbase = 'OU=Users,DC=Contoso,DC=com'
$Server = 'DCserver'
$givenNameList =

$surNameList =

foreach($surName in $surNameList){
    foreach($givenName in $givenNameList){
        Get-ADUser -Filter "enabled -eq '$true' -and surname -eq '$surName' -and givenname -like '*$givenName*'" -SearchBase $Searchbase -Server $Server | 
            Select-Object -Property Name,givenName,surName,sAMAccountName

Sorry I don’t know what happened here as I replied to this message this morning.

First Thank you as it gives me enough of the data to easily complete what I need!
Second and I’m not concerned about I do get a bunch of errors when executing it stating:

Get-ADUser : Error parsing query: ‘enabled -eq ‘True’ -and surname -eq ‘Rien’ -and givenname -like ‘Bob’’ Error Message:
‘syntax error’ at position: ‘40’.

Now that I look at your code it looks like the error is from the mismatch of the data it is searching SurName and different GivenName. I believe I should be able to resolve this by just adding -ErrorAction SilentlyContinue to the Get-ADUser command.

Looks to me like you have a syntax error in your filter string. You have to be careful to choose the right quotes for the filter string. Outside the string you use double quotes and inside single quotes … like this:
-Filter enabled -eq$true -and surname -eq $surName -and givenname -like $givenName

Olaf strange the -ErrorAction SilentlyContinue did not do as I thought and I have the string EXACTLY as you have mentioned it both times. Where I see the error is when it seems like a name does not match since it is going through every fname with the single lname.

My Code:

$SAM = foreach($surName in $lName){
    foreach($givenName in $fName){
        Get-ADUser -Filter "enabled -eq '$true' -and surname -eq '$surName' -and givenname -like '*$givenName*'" -Server $Server -ErrorAction SilentlyContinue  |
            Select-Object -Property Name,givenName,surName,sAMAccountName
$sam | export-csv -notype -path C:\Temp\ADQueries\SAMAccounts.csv

Again it works I can ignore the few errors that come up as they do not affect the value of the data.

I won’t complain. :wink: If you like to eliminate the errors anyway you can add error handling with try catch.