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

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 =="
}

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}

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

 

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 =="
}

Thanks for the quick response. This helpd.