Link to home
Start Free TrialLog in
Avatar of abdull sheriff
abdull sheriff

asked on

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
Avatar of oBdA
oBdA

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

Avatar of abdull sheriff

ASKER

addition to display name can i get the first and last name separately? in the same output file
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

@oBdA you the man, all ran perfectly.
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"
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

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
Then you csv in all likelihood doesn't have a column "DisplayName", or it's empty.
yeah i had a space in the display name, i corrected it and it ran but there is no data, 0kb
Then there are no matches, or you accidentally removed one of the pipes at the end of lines 2 and 3.
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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you so much