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
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"
ASKER
@oBdA you the man, all ran perfectly.
ASKER
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"
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.
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"
ASKER
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 .ActiveDir ectory.Man agement.Co mmands.Get ADUser
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
Then you csv in all likelihood doesn't have a column "DisplayName", or it's empty.
ASKER
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.
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you so much
Open in new window