Renaming AD user accounts distinguished names RRS feed

  • Question

  • Hello, 

    Im very new to coding and Im trying to rename the distinguished name or CN name of my users in a container. I want the the cn name to be the same as the SamAccountName. 

    I have tried the following code but am stuck. 

    Get-ADUser -SearchBase 'OU=test,OU=Users,OU=Prod,OU=RNSW,OU=Agencies,DC=DPI,DC=NSW,DC=GOV,DC=AU' -Filter * -properties distinguishedname | Rename-ADObject $_.DistinguishedName -NewName ($_.samaccountname) 

    Any help would be greatly appreciated. 

    Wednesday, July 24, 2019 5:55 AM

All replies

  • help rename-object -online

    This will rename the CN (Name) of the object (account).

    For how to use AD with PowerShell:

    You can find many books and tutorials to trach you basic PowerShell.

    $searchBase = 'OU=test,OU=Users,OU=Prod,OU=RNSW,OU=Agencies,DC=DPI,DC=NSW,DC=GOV,DC=AU'
    Get-ADUser -SearchBase $searchBase -Filter *| 
    Rename-ADObject $_ -NewName $_.samaccountname


    • Edited by jrv Wednesday, July 24, 2019 7:02 AM
    Wednesday, July 24, 2019 6:53 AM
  • Thankyou for the help. I tried this and got the same error as before:

    Rename-ADObject : Cannot validate argument on parameter 'NewName'. The argument is null or empty. Provide an argument that is not null or empty, 
    and then try the command again.
    At line:4 char:186
    + ... e | Rename-ADObject $_.DistinguishedName -NewName ($_.samaccountname)

    Thursday, July 25, 2019 12:52 AM
  • Works fine for the rest of us.

    Recopy the code and try again.  Be sure you don't make any typos or change anything.


    Thursday, July 25, 2019 1:07 AM
  • Thankyou very much and I understand where I was failing. I appreciate the help. 

    Thursday, July 25, 2019 3:23 AM