Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I need to reconcile the displayName and CN attributes to the givenName and surName fields in AD.

Is it possible to take givenName and surName and rename both the displayName and CN using Powershell?

share|improve this question

1 Answer 1

up vote 9 down vote accepted

Powershell really shines for clean-up tasks like this.

Import-Module ActiveDirectory
$allUsers = Get-ADUser -Filter * -Properties cn,displayName

foreach ( $u in $allUsers | Where-Object { ($_.givenName) -and ($_.surName) } ) {
    $fn = $u.givenName.Trim()
    $ln = $u.surName.Trim()
    Write-Host $fn $ln
    Set-ADUser -Identity $u -DisplayName "$fn $ln" -GivenName "$fn" -SurName "$ln" -PassThru |
        Rename-ADObject -NewName "$fn $ln"
}

Caveats:

  1. I'm Trim()ing the givenName and surName to remove leading/trailing spaces. We commit these two back to the account, as well as correcting displayName and cn.
  2. Scope your -Filter in Get-ADUser to only gather the users you want. The * will grab everyone in the domain, including built-ins, admins, etc. This probably isn't what you want. :)
  3. The Where-Object portion of the foreach will skip user objects that do not have both a givenName and surName attribute.
  4. If you need to play with the renamed user object, add a -PassThru to the end of the Rename-ADObject line and the object will be passed back to you.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.