# samaccountname for the given file of display names

**URL:** <https://forums.powershell.org/t/samaccountname-for-the-given-file-of-display-names/1327>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/samaccountname-for-the-given-file-of-display-names/1327 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/samaccountname-for-the-given-file-of-display-names/1327/1 "2011-12-31T18:30:00Z")

</div>

by athreyatoo43 at 2013-02-11 06:03:25

> can someone help me with the script to get the samaccountname of a list of users by their display names.

by coderaven at 2013-02-11 07:54:26

> I would suggest to use the ActiveDirectory module that comes with the RSAT tools  
>   
> `Import-Module ActiveDirectoryGet-Content -Path \Path\to\File.txt | foreach-object { Get-ADUser -Identity $_ | Select Name, SamAccountName }`

by athreyatoo43 at 2013-02-12 03:52:43

> Does this ouput the file ? if so, where does it ? and what format.  
> I want to give a excel or csv as input and want an excel sheet as output. please help

by athreyatoo43 at 2013-02-12 04:17:23

> I Get the below error  
>   
> PS C:\Windows\system32\> cd\<br\>PS C:&gt; cd users\ushasp\desktop  
> PS C:\users\ushasp\desktop\> Import-Module ActiveDirectory  
> PS C:\users\ushasp\desktop\> Get-Content -Path DIsplay-Names.txt  
> Rajesh Bondu  
> Nakul AR  
> PS C:\users\ushasp\desktop\> foreach-object { Get-ADUser -Identity $\_ | Select Name, SamAccountName }  
> Get-ADUser : Cannot validate argument on parameter ‘Identity’. The argument is null. Supply a non-null argument and try  
> the command again.  
> At line:1 char:38  
> + foreach-object { Get-ADUser -Identity \<\<\<\< $\_ | Select Name, SamAccountName }  
> + CategoryInfo : InvalidData: (🙂 [Get-ADUser], ParameterBindingValidationException  
> + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

by Klaas at 2013-02-12 05:03:07

> If you execute the Get-Content cmdlet alone, the result is written to the screen. After that you use the Foreach cmdlet but there is nothing in the pipe.  
> You really have to use both cmdlets in combination, with the | in between, exactly as coderaven wrote.  
> If you want to write this to a file, add `| export-csv C] at the end.`

by athreyatoo43 at 2013-02-12 07:46:44

> HI Klaas, i am very bad at scripting… could you please give me the complete script at once…

by Klaas at 2013-02-12 08:22:57

> `Import-Module ActiveDirectoryGet-Content -Path C:\users\ushasp\desktop\DIsplay-Names.txt | foreach-object { Get-ADUser -Identity $_ | Select Name, SamAccountName }| export-csv C]`

---

<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:58pm UTC](https://forums.powershell.org/t/samaccountname-for-the-given-file-of-display-names/1327/2 "2024-05-16T20:58:35Z")

</div>


