get-aduser samaccountname from first name and last name

Welcome Forums General PowerShell Q&A get-aduser samaccountname from first name and last name

  • This topic has 7 replies, 2 voices, and was last updated 2 months ago by
    Participant
    .
Viewing 7 reply threads
  • Author
    Posts
    • #213612
      Participant
      Topics: 11
      Replies: 23
      Points: 162
      Rank: Participant

      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!

      Example:

      $FName =
      “John”,
      “Erica”,
      “Amanda”,
      “Mike”

      $lname =
      “Kane”,
      “Tarbet”,
      “Braun”,
      “Hartshorne”

      lname would be -eq and fname would be -like

       

    • #213621
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

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

    • #213717
      Participant
      Topics: 11
      Replies: 23
      Points: 162
      Rank: Participant

      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.

    • #214029
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      This produces a system object failure ….

      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 =
      'John',
      'Erica',
      'Amanda',
      'Mike'
      
      $surNameList =
      'Kane',
      'Tarbet',
      'Braun',
      'Hartshorne'
      
      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
          }
      }
    • #214443
      Participant
      Topics: 11
      Replies: 23
      Points: 162
      Rank: Participant

      Olaf,
      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’.

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

    • #214449
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

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

      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*

    • #214485
      Participant
      Topics: 11
      Replies: 23
      Points: 162
      Rank: Participant

      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.

    • #214488
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      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. 😉 If you like to eliminate the errors anyway you can add error handling with try catch.

Viewing 7 reply threads
  • You must be logged in to reply to this topic.