# get samaccountname from displayname

**URL:** <https://forums.powershell.org/t/get-samaccountname-from-displayname/3606>\
**Category:** PowerShell Help\
**Created:** [January 9, 2015, 12:16am UTC](https://forums.powershell.org/t/get-samaccountname-from-displayname/3606 "2015-01-09T00:16:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wilfredo-perez1](https://avatars.discourse-cdn.com/v4/letter/w/49beb7/32.png) [@wilfredo-perez1](https://forums.powershell.org/u/wilfredo-perez1)\
**Post date:** [January 9, 2015, 12:16am UTC](https://forums.powershell.org/t/get-samaccountname-from-displayname/3606/1 "2015-01-09T00:16:00Z")

</div>

I have a list of user names ex. last name, First name. Here is my script

foreach ($user in (Get-Content -Path U:\Morningstar\Morningstar.txt))  
{Get-ADUser -filter { DisplayName -like “$user” }} | samaccountname

The problem is the -filter part.

---

<div class="post-metadata">

**Author:** ![wilfredo-perez1](https://avatars.discourse-cdn.com/v4/letter/w/49beb7/32.png) [@wilfredo-perez1](https://forums.powershell.org/u/wilfredo-perez1)\
**Post date:** [January 9, 2015, 12:43am UTC](https://forums.powershell.org/t/get-samaccountname-from-displayname/3606/2 "2015-01-09T00:43:44Z")

</div>

it’s working now:

Here is the script:

foreach ($user in (Get-Content -Path U:\Morningstar\Morningstar.txt))  
{Get-ADUser -filter { DisplayName -like $user } | Select SamAccountName }

---

<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:46pm UTC](https://forums.powershell.org/t/get-samaccountname-from-displayname/3606/3 "2024-05-16T20:46:32Z")

</div>


