This article was previously published under Q300427
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes how to change the display names of Active Directory users with Active Directory Services Interface (ADSI) script.
After you migrate users from a Microsoft Windows NT 4.0 domain or another directory, you may have user's display names in the FirstName LastName format. If you want to change this to LastName, Firstname Middle format but the Active Directory users are not populated with First Name Last Names yet (as Q277717 requires), the following script takes the current display name, splits it into separate strings, rearranges them, and then writes them back to display names in the LastName, First Middle format. It also populates the LastName (SN), FirstName (givenName) and Middlename properties of the user.
This script does not populate the middle initials fields, instead, it appends anything after Fname Lastname to the end of the string. For example, Jane Doe is changed to Doe, Jane and John C. Doe is changed to Doe, John C., but the initials field is not populated.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
rem chgdisplay3.vbs - Changes the display names of all users in a given OU to the rem format of Lastname, Firstname Middle using the current displayName field and breaking this into seperate strings.rem Usage = cscript chgdisplay.vbs "OU=My Ou, DC=My Domain, DC=com"rem OU must be enclosed in quotes if it contains spaces in the nameDim strTargetOUParseCommandLine()wscript.echo strTargetOUwscript.echowscript.echo "Changing Display names of users in " & strTargetOUSet oTargetOU = GetObject("LDAP://" & strTargetOU)oTargetOU.Filter = Array("user")For each usr in oTargetOU if instr(usr.SamAccountName, "$") = 0 then if instr(usr.displayName, ",") = 0 then vTempName = usr.get("displayName") astrWords = split(vTempName) CountWords = UBound(astrWords) - LBound(astrWords) +1 if CountWords = 2 then vFirst = astrWords(0) vLast = astrWords(1) vFullname = vLast + ", " + vFirst usr.put "Sn", vLast usr.put "GivenName", vFirst end if if CountWords = 3 then vFirst = astrWords(0) vMiddle = astrWords(1) vLast = astrWords(2) vFullname = vLast + ", " + vFirst + " " + vMiddle usr.put "Sn", vLast usr.put "GivenName", vFirst usr.put "middleName", vMiddle end if usr.put "displayName", vFullName usr.setinfo wscript.echo vFullName end if end ifNextSub ParseCommandLine() Dim vArgs set vArgs = WScript.Arguments if vArgs.Count <> 1 then DisplayUsage() Else strTargetOU = vArgs(0) End ifEnd SubSub DisplayUsage() WScript.Echo WScript.Echo "Usage: cscript.exe " & WScript.ScriptName & " <Target OU to change users display names in>" WScript.Echo "Example: cscript " & WScript.ScriptName & " " & chr(34) & "OU=MyOU,DC=MyDomain,DC=com" & chr(34) WScript.Quit(0)End Sub