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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abdull sheriffSystems EngineerAuthor Commented:
thank you so much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.