Monthly Archives: May 2014

Rename AD User

With this script you can rename all user in active directory.
the new name is composed using the properties LastName and FirstName and joined with a space ( space )

1. Get all user in the AD domain
We use the LDAPFilter so we retrieve only user with with FirstName (givenName) and LastName (sn) compiled
$adUsers = Get-ADUser -LDAPFilter "(&(objectCategory=person)(objectClass=user)(givenName=*)(sn=*))"
2. Rename the user
The rename process was a 2 step process. First we change the Display Name and second we change the name of the user

$adDomain = domain.local
$adUsers | Foreach-Object {
$dn = $_.Surname.Trim() + ' ' + $_.GivenName.Trim()
$_.DisplayName = $dn
Set-AdUser -Instance $_
Try {
Rename-ADObject -identity $_.DistinguishedName -Newname $dn -Server $adDomain
}
Catch {
Write-Host "$_.Name may already exist."
}
}

This script was tested on Windows Server 2008 R2 domain controller