locked
how to rename AD User Name RRS feed

  • Question

  • Hi,

    I have 400+ AD users, Could anyone please provide a script to update/rename all the CN name for them. 


    Shekar-Technet

    Monday, March 18, 2019 7:01 PM

Answers

  • Here is a script used in the past to change the cn attribute (the RDN) of users. The code you use depends on how you want to rename the users. This example retrieves first and last names (if assigned) and renames the users based on those values.

    # Find all users in the search base that have a first (GivenName) and last name (Surname) assigned in AD.
    # The new Relative Distinguished Name (RDN) will be "Firstname Lastname".
    $Users = Get-ADUser -SearchBase "ou=Sales,ou=West,dc=Domain,dc=com" -Filter {(GivenName -Like "*") -And (Surname -Like "*")} | Select GivenName, Surname, Name, DistinguishedName
    # Enumerate the users.
    ForEach ($User In $Users)
    {
        # Retrieve values for the user.
        $DN = $User.DistinguishedName
        $First = $User.GivenName
        $Last = $User.Surname
        $CN = $User.Name
        $NewName = "$First $Last"
        # Only rename the user if necessary.
        If ($CN -ne $NewName) {Rename-ADObject -Identity $DN -NewName $NewName}
    }

    Another example renames the users based on values in a CSV file.

    # The CSV file has a header line defining the fields sAMAccountName and RDN.
    # sAMAccountName specifies the user, RDN is the new value of the cn attribute.
    $Users = Import-Csv c:\scripts\users.csv
    ForEach ($User In $Users)
    {
        # Retrieve values from the CSV for this user.
        $ID = $($User.sAMAccountName)
        $NewName = $($User.RDN)
        # Find the existing value of the cn attribute and the distinguishedName of this user.
        $ADUser Get-ADUser -Identity $ID | Select DistinguishedName, Name
        $CN = $ADUser.Name
        $DN = $ADUser.DistinguishedName
        # Only rename the user if necessary.
        If ($CN -ne $NewName) {Rename-ADObject -Identity $DN -NewName $NewName}
    }


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, March 19, 2019 3:52 AM

All replies

  • Hello,

    Please chek the following link from TechNet.

    https://gallery.technet.microsoft.com/scriptcenter/RENAME-AN-ACTIVE-DIRECTORY-47e79efb


    Mark it as answer if your question has solved. MCT Regional Lead. x2 MCSE-MCSA Exchange Server & Windows Server

    Monday, March 18, 2019 7:03 PM
  • You can use Rename-ADObject and Set-ADUser cmdlets to do it. It depends how you will import the new names but if it is from a CSV you can use import-CSV cmdlet as well.

    This posting is provided AS IS with no warranties or guarantees , and confers no rights.

    Ahmed MALEK

    My Website Link

    My Linkedin Profile

    My MVP Profile

    Monday, March 18, 2019 10:39 PM
  • I don't believe the Set-ADUser cmdlet can modify the RDN of a user. I believe you must use Rename-ADObject.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Monday, March 18, 2019 11:12 PM
  • Here is a script used in the past to change the cn attribute (the RDN) of users. The code you use depends on how you want to rename the users. This example retrieves first and last names (if assigned) and renames the users based on those values.

    # Find all users in the search base that have a first (GivenName) and last name (Surname) assigned in AD.
    # The new Relative Distinguished Name (RDN) will be "Firstname Lastname".
    $Users = Get-ADUser -SearchBase "ou=Sales,ou=West,dc=Domain,dc=com" -Filter {(GivenName -Like "*") -And (Surname -Like "*")} | Select GivenName, Surname, Name, DistinguishedName
    # Enumerate the users.
    ForEach ($User In $Users)
    {
        # Retrieve values for the user.
        $DN = $User.DistinguishedName
        $First = $User.GivenName
        $Last = $User.Surname
        $CN = $User.Name
        $NewName = "$First $Last"
        # Only rename the user if necessary.
        If ($CN -ne $NewName) {Rename-ADObject -Identity $DN -NewName $NewName}
    }

    Another example renames the users based on values in a CSV file.

    # The CSV file has a header line defining the fields sAMAccountName and RDN.
    # sAMAccountName specifies the user, RDN is the new value of the cn attribute.
    $Users = Import-Csv c:\scripts\users.csv
    ForEach ($User In $Users)
    {
        # Retrieve values from the CSV for this user.
        $ID = $($User.sAMAccountName)
        $NewName = $($User.RDN)
        # Find the existing value of the cn attribute and the distinguishedName of this user.
        $ADUser Get-ADUser -Identity $ID | Select DistinguishedName, Name
        $CN = $ADUser.Name
        $DN = $ADUser.DistinguishedName
        # Only rename the user if necessary.
        If ($CN -ne $NewName) {Rename-ADObject -Identity $DN -NewName $NewName}
    }


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, March 19, 2019 3:52 AM