Set a Users Active Directory Display Name with PowerShell

I recently saw an article on how to set a users Active Directory display name based on the values of their given name, initials, and surname. I came up with my own unique solution for this task and thought I would share it with you, the readers of my blog.

As you can see in the following example, there are a mixture of users who need their display name corrected based on the requirement that their display name be listed as Givenname Initials Surname:

1Get-ADUser -Filter * -SearchBase 'OU=Northwind Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' -Properties DisplayName, Initials |
2Select-Object -Property DisplayName, GivenName, Initials, Surname

displayname1.png

I use a simple one-liner to correct the display name of all of the users in the Northwind Active Directory OU (Organizational Unit). One issue though, is that not all users have a middle initial and those users will end up with two spaces in their display name between their first and last name. Instead of using logic to correct that issue, I decided to use a regular expression that matches one or more spaces that are followed by a space:

1Get-ADUser -Filter * -SearchBase 'OU=Northwind Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' -Properties Initials |
2ForEach-Object {
3    Set-ADUser -Identity $_ -DisplayName ("$($_.GivenName) $($_.Initials) $($_.Surname)" -replace '\s+(?=\s)')
4}

displayname2.png

The previous command is indeed a PowerShell one-liner even though it is on more than one physical line and formatted for readability. A PowerShell one-liner is a command that is one continuous pipeline regardless of how many physical lines it is on. A PowerShell one-liner is NOT defined by the lack of carriage returns <period>.

1Get-ADUser -Filter * -SearchBase 'OU=Northwind Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' -Properties DisplayName, Initials |
2Select-Object -Property DisplayName, GivenName, Initials, Surname

displayname3.png

As you can see in the previous set of results, this task is complete and although only nine users were updated in this example, it could have just as easily been 900 users.

µ