Category Archives: powershell

Change Active Directory DisplayName Script

I recently imported users to Active Directory and made a mistake on the import script. I accidentally swapped the first name and last name within the Name and DisplayName fields for a few hundred users. I didn’t really want to fix this manually- so I did some digging and pieced together a couple of scripts to do just what I needed within powershell.

This is what I had:

Name: Hunter Daren
DisplayName: Hunter Daren
GivenName: Daren
Surname: Hunter

Basically – I had the correct Given and Surname – but mixed them up in the script for DisplayName and Name.

  1. Open windows powershell with administrator privileges.
  2. You will need to import Active Directory Module. import-module activedirectory before you can run the commands below.
  3. To swap the DisplayName – I created the following script:
    Get-ADUser -SearchBase ‘OU=Your,OU=Organizational,DC=test,DC=com’ -Filter * -Properties displayname | % {Set-ADUser -Identity $_ -DisplayName (“{0} {1}” -f $_.GivenName,$_.Surname)}You will just need to swap out your SearchBase with the OU information where your users are located.
  4. Here is the script I used to change the Name in Active Directory:Get-ADUser -SearchBase 'OU=Your,OU=Organizational,DC=test,DC=com' -Filter * -Properties displayname | % {Rename-ADObject -Identity $_ -NewName ("{0} {1}" -f $_.GivenName,$_.Surname)}

These two Active Directory Scripts will change the Name and DisplayName for all users in the specified OU. Keep in mind – I was changing to first name and last name.

You can change the ("{0} {1}" -f $_.GivenName,$_.Surname) to meet your specific needs. ("{0} {1}" -f $_.GivenName,$_.Surname) is just concatenating the First Name and Last Name together with a space between.

For example – you might want to useĀ ("{0}.{1}@{2}" -f $_.GivenName,$_.Surname,"") which would give you