VBScript - How to Rename Active Directory User Name

Environment
  • Microsoft Active Directory

Version
  • 2010-Jul-08

Introduction
Rename user accounts to be the wanted standard

User list should like this
Old Name,New Name,User Principal,SAM Account,First Name,Last Name Lili,Lili.Peng,Lili.Peng,Lili.Peng,Lili,Peng
Due to have to let user know this firstly, running the rename would be later than notification and not working hour


Code
This renaming is run on the domain controller
Const DEFAULT_USER_LIST = "\\Server\netlogon\tmp\username.csv"
Const DEFAULT_LDAP = "ou=Home,dc=domain,dc=local"
Const DEFAULT_USERPRINAME = "@domain.local"

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTStream = objFSO.OpenTextFile(DEFAULT_USER_LIST, 1, 1)
Set objUsers = GetObject("LDAP://" & DEFAULT_LDAP)

Do While objTStream.AtEndOfStream <> True
    arline = Split(objTStream.ReadLine, ",")

    'Rename CN
    Set objUser = objUsers.moveHere("LDAP://cn=" & arline(0) & "," & DEFAULT_LDAP, "cn=" & arline(1))

    'Ensure to set the renamed accounts
    If objUser.CN = arline(1) Then
        'Rename user logon name and display name
        objUser.userPrincipalName = arline(2) & DEFAULT_USERPRINAME
        objUser.sAMAccountName = arline(3)    'pre-Windows 2000 logon
        objUser.givenName = arline(4)    'First name
        objUser.SN = arline(5)    'Last name
        objUser.displayName = arline(4) & " " & arline(5)
       
        WSCript.Echo objUser.CN & " " & objUser.userPrincipalName
        objUser.SetInfo
    End If
Loop

objTStream.close
Also notify the user, whose user name would be scheduled to change, by login script
Const DEFAULT_USER_LIST = "\\Server\netlogon\tmp\username.csv"

On Error Resume Next
Set objntw = createobject("WScript.Network")
Set objshell= createobject("WScript.Shell")

strUser = UCase(objntw.Username)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTStream = objFSO.OpenTextFile(DEFAULT_USER_LIST, 1, 1)

Do While objTStream.AtEndOfStream <> True
    arline = Split(objTStream.ReadLine, ",")
   
    If strUser = UCase(arline(0)) Then
        newUser = arline(1)
        msgbox("Your New Windows User Name is scheduled to be changed tonight!" & vbNewLine & vbNewLine & arline(1))
    End If
Loop

objTStream.close