How to change Display Name a Full Name by script from .csv

Jiří Trenkler 21 Reputation points
2022-01-31T06:59:41.827+00:00

Hello, I would like to ask you for a help. I have a company where almost everyone has mistake in display name and full name.... Someone did it by across. It means that sometimes, it rights. But at most cases its different, instead surname and name its name and surname.
I have an .csv with that names. Can anyone please help me with script for make this right please?
Best regards

Jiri

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-01-31T18:12:39.897+00:00

    Hi @Jiří Trenkler ,

    do you have started with a PowerShell script? If so please post it here using the Code Sample option (the icon with 101010).

    If you don't have a script please take a look here to get started:
    https://lazyadmin.nl/powershell/using-powershell-to-update-an-ad-user-from-a-csv-file
    https://devblogs.microsoft.com/scripting/use-powershell-to-set-ad-ds-users-display-names/

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Jiří Trenkler 21 Reputation points
    2022-02-01T07:40:26.577+00:00

    Hi @Andreas Baumgarten ,
    thank you for your answer. I never do that by powershelgl script so that's why iam asking for a help. Until this time I did It manually. But now its company about 1000 employees and now iam not able to do that. For understanding. The admin which were there before me, created employees for example surname:Novak first name: Jiri and almost 300 is correct but the rest of them, he did like surname: Jiri. first name : Novak. So he did mistake and change name and surname. So name of user is NovakJiri. and someone is JiriNovak. And I want to make it right . Did I explain my problem correctly ?
    Best regards

    Jiří Trenkler


  2. Jiří Trenkler 21 Reputation points
    2022-02-07T07:03:18.46+00:00

    Dear Mr. @Andreas Baumgarten iam so sorry for disturbing you again but iam lost :(( I created a tested lab with few members. and iam still not able to change it. Can you help me please? I did this after few hours and this work for me but next step is impossible for me.

    Split path

    $Path = Split-Path -Parent "C:\scripts*.*"

    Create variable for the date stamp in log file

    $LogDate = Get-Date -f yyyyMMddhhmm

    Define CSV and log file location variables

    They have to be on the same location as the script

    $Csvfile = $Path + "\AllADUsers_$logDate.csv"

    Import Active Directory module

    Import-Module ActiveDirectory

    Set distinguishedName as searchbase, you can use one OU or multiple OUs

    Or use the root domain like DC=exoip,DC=local

    $DNs = @(
    "OU=Users,OU=LAB,DC=lab,DC=local"

    )

    Create empty array

    $AllADUsers = @()

    Loop through every DN

    foreach ($DN in $DNs) {
    $Users = Get-ADUser -SearchBase $DN -Filter * -Properties *

    # Add users to array  
    $AllADUsers += $Users  
    

    }

    Create list

    $AllADUsers | Sort-Object Name | Select-Object `
    @{Label = "First name"; Expression = { $.GivenName } },
    @{Label = "Last name"; Expression = { $
    .Surname } },
    @{Label = "Display name"; Expression = { $.DisplayName } },
    @{Label = "User logon name"; Expression = { $
    .SamAccountName } },
    @{Label = "User principal name"; Expression = { $.UserPrincipalName } },
    @{Label = "Street"; Expression = { $
    .StreetAddress } },
    @{Label = "City"; Expression = { $.City } },
    @{Label = "State/province"; Expression = { $
    .State } },
    @{Label = "Zip/Postal Code"; Expression = { $.PostalCode } },
    @{Label = "Country/region"; Expression = { $
    .Country } },
    @{Label = "Job Title"; Expression = { $.Title } },
    @{Label = "Department"; Expression = { $
    .Department } },
    @{Label = "Company"; Expression = { $.Company } },
    @{Label = "Manager"; Expression = { % { (Get-AdUser $
    .Manager -Properties DisplayName).DisplayName } } },
    @{Label = "Description"; Expression = { $.Description } },
    @{Label = "Office"; Expression = { $
    .Office } },
    @{Label = "Telephone number"; Expression = { $.telephoneNumber } },
    @{Label = "E-mail"; Expression = { $
    .Mail } },
    @{Label = "Mobile"; Expression = { $.mobile } },
    @{Label = "Notes"; Expression = { $
    .info } },
    @{Label = "Account status"; Expression = { if (($.Enabled -eq 'TRUE') ) { 'Enabled' } Else { 'Disabled' } } },
    @{Label = "Last logon date"; Expression = { $
    .lastlogondate } }|

    Export report to CSV file

    Export-Csv -Encoding UTF8 -Path $Csvfile -NoTypeInformation #-Delimiter ";"

    So I have CSV but now I don't know what to do. it should be easy but iam lost. Thank you for your help again. Jiri


  3. Jiří Trenkler 21 Reputation points
    2022-02-07T08:02:47.26+00:00
    # Split path
    $Path = Split-Path -Parent "C:\scripts\*.*"
    
    # Create variable for the date stamp in log file
    $LogDate = Get-Date -f yyyyMMddhhmm
    
    # Define CSV and log file location variables
    # They have to be on the same location as the script
    $Csvfile = $Path + "\AllADUsers_$logDate.csv"
    
    # Import Active Directory module
    Import-Module ActiveDirectory
    
    # Set distinguishedName as searchbase, you can use one OU or multiple OUs
    # Or use the root domain like DC=exoip,DC=local
    $DNs = @(
        "OU=Users,OU=LAB,DC=lab,DC=local"
    
    )
    
    # Create empty array
    $AllADUsers = @()
    
    # Loop through every DN
    foreach ($DN in $DNs) {
        $Users = Get-ADUser -SearchBase $DN -Filter * -Properties * 
    
        # Add users to array
        $AllADUsers += $Users
    }
    
    # Create list
    $AllADUsers | Sort-Object Name | Select-Object `
    @{Label = "First name"; Expression = { $_.GivenName } },
    @{Label = "Last name"; Expression = { $_.Surname } },
    @{Label = "Display name"; Expression = { $_.DisplayName } },
    @{Label = "User logon name"; Expression = { $_.SamAccountName } },
    @{Label = "User principal name"; Expression = { $_.UserPrincipalName } },
    @{Label = "Street"; Expression = { $_.StreetAddress } },
    @{Label = "City"; Expression = { $_.City } },
    @{Label = "State/province"; Expression = { $_.State } },
    @{Label = "Zip/Postal Code"; Expression = { $_.PostalCode } },
    @{Label = "Country/region"; Expression = { $_.Country } },
    @{Label = "Job Title"; Expression = { $_.Title } },
    @{Label = "Department"; Expression = { $_.Department } },
    @{Label = "Company"; Expression = { $_.Company } },
    @{Label = "Manager"; Expression = { % { (Get-AdUser $_.Manager -Properties DisplayName).DisplayName } } },
    @{Label = "Description"; Expression = { $_.Description } },
    @{Label = "Office"; Expression = { $_.Office } },
    @{Label = "Telephone number"; Expression = { $_.telephoneNumber } },
    @{Label = "E-mail"; Expression = { $_.Mail } },
    @{Label = "Mobile"; Expression = { $_.mobile } },
    @{Label = "Notes"; Expression = { $_.info } },
    @{Label = "Account status"; Expression = { if (($_.Enabled -eq 'TRUE') ) { 'Enabled' } Else { 'Disabled' } } },
    @{Label = "Last logon date"; Expression = { $_.lastlogondate } }|
    
    # Export report to CSV file
    Export-Csv -Encoding UTF8 -Path $Csvfile -NoTypeInformation #-Delimiter ";"
    
    0 comments No comments

  4. Jiří Trenkler 21 Reputation points
    2022-02-07T08:07:29.927+00:00
    # Split path
    $Path = Split-Path -Parent "C:\scripts\*.*"
    
    # Create variable for the date stamp in log file
    $LogDate = Get-Date -f yyyyMMddhhmm
    
    # Define CSV and log file location variables
    # They have to be on the same location as the script
    $Csvfile = $Path + "\AllADUsers_$logDate.csv"
    
    # Import Active Directory module
    Import-Module ActiveDirectory
    
    # Set distinguishedName as searchbase, you can use one OU or multiple OUs
    # Or use the root domain like DC=exoip,DC=local
    $DNs = @(
        "OU=Users,OU=LAB,DC=lab,DC=local"
    
    )
    
    # Create empty array
    $AllADUsers = @()
    
    # Loop through every DN
    foreach ($DN in $DNs) {
        $Users = Get-ADUser -SearchBase $DN -Filter * -Properties * 
    
        # Add users to array
        $AllADUsers += $Users
    }
    
    # Create list
    $AllADUsers | Sort-Object Name | Select-Object `
    @{Label = "First name"; Expression = { $_.GivenName } },
    @{Label = "Last name"; Expression = { $_.Surname } },
    @{Label = "Display name"; Expression = { $_.DisplayName } },
    @{Label = "User logon name"; Expression = { $_.SamAccountName } },
    @{Label = "User principal name"; Expression = { $_.UserPrincipalName } },
    @{Label = "Street"; Expression = { $_.StreetAddress } },
    @{Label = "City"; Expression = { $_.City } },
    @{Label = "State/province"; Expression = { $_.State } },
    @{Label = "Zip/Postal Code"; Expression = { $_.PostalCode } },
    @{Label = "Country/region"; Expression = { $_.Country } },
    @{Label = "Job Title"; Expression = { $_.Title } },
    @{Label = "Department"; Expression = { $_.Department } },
    @{Label = "Company"; Expression = { $_.Company } },
    @{Label = "Manager"; Expression = { % { (Get-AdUser $_.Manager -Properties DisplayName).DisplayName } } },
    @{Label = "Description"; Expression = { $_.Description } },
    @{Label = "Office"; Expression = { $_.Office } },
    @{Label = "Telephone number"; Expression = { $_.telephoneNumber } },
    @{Label = "E-mail"; Expression = { $_.Mail } },
    @{Label = "Mobile"; Expression = { $_.mobile } },
    @{Label = "Notes"; Expression = { $_.info } },
    @{Label = "Account status"; Expression = { if (($_.Enabled -eq 'TRUE') ) { 'Enabled' } Else { 'Disabled' } } },
    @{Label = "Last logon date"; Expression = { $_.lastlogondate } }|
    
    # Export report to CSV file
    Export-Csv -Encoding UTF8 -Path $Csvfile -NoTypeInformation #-Delimiter ";"
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.