Active Directory Script – update object name or displayname

Get-Content .\usersToUpdate.txt |
Get-ADUser -Properties middleName |
ForEach {
# The next lines are used to the the “Display Name” seen in ADUC. Comment these out if you do not want to update the DisplayName
If ($_.middleName -eq $null) {
Set-ADUser $_ -DisplayName ($_.GivenName + ” ” + $_.Surname)
}
Else {
Set-ADUser $_ -DisplayName ($_.GivenName + ” ” + $_.middleName + ” ” + $_.Surname)
}
# These lines set the Name attribute (“Full Name” attribute in ADUC)
If ($_.middleName -eq $null) {
Rename-ADObject $_.DistinguishedName -NewName ($_.GivenName + ” ” + $_.Surname)
}
Else {
Rename-ADObject $_.DistinguishedName -NewName ($_.GivenName + ” ” + $_.middleName + ” ” + $_.Surname)
}
}

This will read in a list of sAMAccountNames (one per line) from a text file named usersToUpdate.txt and then make the changes. Comment out the displayname lines if you only need to update the Name and vice versa. It will check for the presence of a middleName to be sure that an extra space isn’t introduced.

Share
Ryan Wilson
Azure and Office 365 Technical Architect | Microsoft Certified Solutions Associate: Office 365 | Microsoft Certified: Azure Solutions Architect Expert | Microsoft Certified Solutions Expert: Productivity | Microsoft 365 Certified: Enterprise Administrator Expert | Microsoft 365 Certified: Messaging Administrator Associate

Leave a Reply

Your email address will not be published. Required fields are marked *