Find SamAccountName based on DisplayName - combining variables, object to string

Welcome Forums General PowerShell Q&A Find SamAccountName based on DisplayName - combining variables, object to string

This topic contains 4 replies, has 4 voices, and was last updated by

Tim
 
Participant
4 weeks ago.

  • Author
    Posts
  • #112430
    Tim

    Participant
    Points: 0
    Rank: Member

    I'm processing a CSV with first and last names through a ForEach loop so I can grab SamAccountName for employees from an HR spreadsheet.

    I'm trying to combine the First and Last name into a DisplayName that can be used by Get-ADUser.  Currently Get-ADUser doesn't seem to be able to use the variable being passed to it.

    It appears that Get-ADUser wants a string and I'm giving it an object.  I toyed with Out-String but that didn't get me anywhere.

    If I substitute a name in the Get-ADUser line (16) ('John Doe' instead of $ADDisplayName) then this correctly outputs the SamAccountName.

    So the issue seems to be what is being passed to Get-ADUser in the variable for DisplayName.

    Any suggestions would be greatly appreciated!

     

    # Import CSV into variable $Users
    $Users = Import-csv 'R:\IN\Find-EmployeeADAccount.csv'
    
    # Loop through each row of $Users
    ForEach ($User in $Users)
    {
         # Create a string variable to search with Get-ADUser
         $ADGivenName = $User.First
         $ADSurname = $User.Last
         Set-Variable -Name "ADDisplayName" -Value "$ADGivenName $ADSurname"
    
         # Report account being processed
         Write-Host "Finding account for $ADDisplayName."
    
         # Get the user
         $ADAccountObject = Get-ADUser -Filter {DisplayName -like "$ADDisplayName"} -Properties *
         $ADUserAccount = $ADAccountObject.SamAccountName
         Write-Host "== $ADUserAccount =="
    }
    
  • #112432

    Participant
    Points: 25
    Rank: Member

    Remove the qoutes in the filter and you're using LIKE, so if you are doing a wildcard search, then you need to add a asterisk:

    $DisplayName = 'Rob Simmers*'
    
    Get-ADUser -Filter {DisplayName -like $DisplayName}
    
    • #112516
      Tim

      Participant
      Points: 0
      Rank: Member

      Thanks for the quick response.  This helpd.

  • #112433

    Participant
    Points: 8
    Published
    Rank: Member

    This works for me....remove the ""

    $ADGivenName = "Iain"
    
    $ADSurname = "Barnetson"
    
    Set-Variable -Name ADDisplayName -Value $($ADGivenName +" "+ $ADSurname)
    
    Get-ADUser -Filter {DisplayName -like $ADDisplayName}
    
    

     

    The above returns

    DistinguishedName : CN=Iain Barnetson,OU=TechnologyServices,OU=IT,OU=Houston,OU=Users,OU=User Accounts,DC=ebsi,DC=corp
    
    Enabled: True
    
    GivenName: Iain
    
    Name: Iain Barnetson
    
    ObjectClass: user
    
    ObjectGUID: ecfbc29a-afa8-427e-9f4f-b46d0516be33
    
    SamAccountName: ibarnetson
    
    SID: S-1-5-21-1601737501-2236276997-3000204151-16706
    
    Surname: Barnetson
    
    UserPrincipalName: ibarnetson@me.com
    
    

     

  • #112469

    Participant
    Points: 0
    Rank: Member

    I have made some changes on your code. Check it out.

     

    # Import CSV into variable $Users
    $Users = Import-csv 'R:\IN\Find-EmployeeADAccount.csv'
    
    # Loop through each row of $Users
    ForEach ($User in $Users)
    {
         # Create a string variable to search with Get-ADUser
         $ADGivenName = $User.First
         $ADSurname = $User.Last
         $ADDisplayName = "$ADGivenName" + " " + "$ADSurname"
    
         # Report account being processed
         Write-Host "Finding account for $ADDisplayName."
    
         # Get the user
         $ADAccountObject = (Get-ADUser -Filter {DisplayName -like "$ADDisplayName"} -Properties SamAccountName).SamAccountName
         Write-Host "== $ADAccountObject =="
    }
    

You must be logged in to reply to this topic.