Power shell Scrpit for getting the display name

I have a list of samaccount name of the users in a csv file, i need a Power shell script for getting their DISPLAY NAME and the output in a new csv file with samaccountname and display name from AD
abdull sheriffSystems EngineerAsked:
Who is Participating?
 
oBdACommented:
Again: then there are no matches; make sure the display names in your csv match the one in AD. Not a script issue.
PS C:\> gc c:\temp\input.csv
DisplayName
john doe
PS C:\> Import-Csv -Path "C:\Temp\input.csv" |
>> ForEach-Object {Get-ADUser -Filter "displayName -eq '$($_.DisplayName)'" -Property DisplayName} |
>> Select-Object -Property SamAccountName, DisplayName |
>> Export-Csv -NoTypeInformation -Path "C:\Temp\output.csv"
>>
PS C:\> gc c:\temp\output.csv
"SamAccountName","DisplayName"
"jdoe","John Doe"

Open in new window

0
 
oBdACommented:
Assuming the input csv has a header line with a column "SamAccountName":
Import-Csv -Path "C:\Temp\input.csv" |
	ForEach-Object {Get-ADUser -Identity $_.SamAccountName -Property DisplayName} |
	Select-Object -Property SamAccountName, DisplayName |
	Export-Csv -NoTypeInformation -Path "C:\Temp\output.csv"

Open in new window

1
 
abdull sheriffSystems EngineerAuthor Commented:
addition to display name can i get the first and last name separately? in the same output file
0
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

 
oBdACommented:
Import-Csv -Path "C:\Temp\input.csv" |
	ForEach-Object {Get-ADUser -Identity $_.SamAccountName -Property displayName, givenName, sn} |
	Select-Object -Property SamAccountName, DisplayName, @{n='FirstName'; e={$_.givenName}}, @{n='LastName'; e={$_.sn}} |
	Export-Csv -NoTypeInformation -Path "C:\Temp\output.csv" 

Open in new window

2
 
abdull sheriffSystems EngineerAuthor Commented:
@oBdA you the man, all ran perfectly.
0
 
abdull sheriffSystems EngineerAuthor Commented:
i am having a csv file with Display name and trying to get their the SamAccountname and below is what i am using but gives an error

Import-Csv -Path "C:\Temp\input.csv" |
      ForEach-Object {Get-ADUser -Identity $_.DisplayName -Property SamAccountName} |
      Select-Object -Property DisplayName, SamAccountName |
      Export-Csv -NoTypeInformation -Path "C:\Temp\output.csv"
0
 
oBdACommented:
The display name is not unique and can not be used for the -Identity argument. -Identity can be SamAccountName, DistinguishedName, or UPN.
All you can do is use a filter - but be aware that this might result in several objects returned for one display name.
Import-Csv -Path "C:\Temp\input.csv" |
	ForEach-Object {Get-ADUser -Filter "displayName -eq '$($_.DisplayName)'" -Property DisplayName} |
	Select-Object -Property SamAccountName, DisplayName |
	Export-Csv -NoTypeInformation -Path "C:\Temp\output.csv" 

Open in new window

0
 
abdull sheriffSystems EngineerAuthor Commented:
i get the below error

Get-ADUser : The search filter cannot be recognized
At line:2 char:18
+     ForEach-Object {Get-ADUser -Filter "displayName -eq '$($_.DisplayName)'" -Prope ...
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
0
 
oBdACommented:
Then you csv in all likelihood doesn't have a column "DisplayName", or it's empty.
0
 
abdull sheriffSystems EngineerAuthor Commented:
yeah i had a space in the display name, i corrected it and it ran but there is no data, 0kb
0
 
oBdACommented:
Then there are no matches, or you accidentally removed one of the pipes at the end of lines 2 and 3.
0
 
abdull sheriffSystems EngineerAuthor Commented:
you mean there is no samaccountname ? i am sure they have

Below is what i ran

Import-Csv -Path "C:\Temp\input.csv" |
      ForEach-Object {Get-ADUser -Filter "displayName -eq '$($_.DisplayName)'" -Property DisplayName} |
      Select-Object -Property SamAccountName, DisplayName |
      Export-Csv -NoTypeInformation -Path "C:\Temp\output.csv"

there is no error when i run the above and there is no data in the output file
0
 
abdull sheriffSystems EngineerAuthor Commented:
thank you so much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.