Vbscript to change local user account Name

Version 1
    Share:|

    Below is the simple code to change local windows user account name to new name.

     

    Save the below code as test.vbs and run the below command:

     

    cscript "c:\temp\test.vbs" OLD_NAME NEW_NAME

     

    #################################################################

     

    sOldUser = WScript.Arguments(0)

    sNewUser = WScript.Arguments(1)

     

     

    strComputer = "."

     

     

    Set colUsers = GetObject("winmgmts:" _

    & "{impersonationLevel=impersonate}!\\" _

    & strComputer & "\root\cimv2").ExecQuery _

    ("select Name from Win32_UserAccount where name = '" & sOldUser & _

    "'")

    For Each objUser In colUsers

    objUser.Rename(sNewUser)

    Next

     

     

    Set objUser = GetObject("WinNT://" & strComputer & "/" & sNewUser & ",user")

    objUser.FullName = sNewUser

    objUser.SetInfo

     

    ###########################################################################