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

**URL:** <https://forums.powershell.org/t/find-samaccountname-based-on-displayname-combining-variables-object-to-string/11357>\
**Category:** PowerShell Help\
**Created:** [September 21, 2018, 4:40pm UTC](https://forums.powershell.org/t/find-samaccountname-based-on-displayname-combining-variables-object-to-string/11357 "2018-09-21T16:40:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmorrisxyz](https://avatars.discourse-cdn.com/v4/letter/t/ac91a4/32.png) [@tmorrisxyz](https://forums.powershell.org/u/tmorrisxyz)\
**Post date:** [September 21, 2018, 4:40pm UTC](https://forums.powershell.org/t/find-samaccountname-based-on-displayname-combining-variables-object-to-string/11357/1 "2018-09-21T16:40:09Z")

</div>

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!

&nbsp;

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

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [September 21, 2018, 4:55pm UTC](https://forums.powershell.org/t/find-samaccountname-based-on-displayname-combining-variables-object-to-string/11357/2 "2018-09-21T16:55:22Z")

</div>

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}
```

---

<div class="post-metadata">

**Author:** ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)\
**Post date:** [September 21, 2018, 4:56pm UTC](https://forums.powershell.org/t/find-samaccountname-based-on-displayname-combining-variables-object-to-string/11357/3 "2018-09-21T16:56:00Z")

</div>

This works for me…remove the “”

```
$ADGivenName = "Iain"

$ADSurname = "Barnetson"

Set-Variable -Name ADDisplayName -Value $($ADGivenName +" "+ $ADSurname)

Get-ADUser -Filter {DisplayName -like $ADDisplayName}
```

&nbsp;

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
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![sconstantinou](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sconstantinou/32/287_2.png) [@sconstantinou](https://forums.powershell.org/u/sconstantinou)\
**Post date:** [September 22, 2018, 9:08am UTC](https://forums.powershell.org/t/find-samaccountname-based-on-displayname-combining-variables-object-to-string/11357/4 "2018-09-22T09:08:07Z")

</div>

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

&nbsp;

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

---

<div class="post-metadata">

**Author:** ![tmorrisxyz](https://avatars.discourse-cdn.com/v4/letter/t/ac91a4/32.png) [@tmorrisxyz](https://forums.powershell.org/u/tmorrisxyz)\
**Post date:** [September 24, 2018, 11:43am UTC](https://forums.powershell.org/t/find-samaccountname-based-on-displayname-combining-variables-object-to-string/11357/5 "2018-09-24T11:43:08Z")

</div>

Thanks for the quick response. This helpd.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/find-samaccountname-based-on-displayname-combining-variables-object-to-string/11357/6 "2024-05-16T20:34:29Z")

</div>


