Rename Users with VBScript

This question came through the red phone line (well email)…. (and there were two questions but I’ll save the other for a different post….)
And it’s quite simple but we are going to cheat here and use a net user command.

So we need to determine a few things well just one thing, Is the user name constant name or does it change?

So this is how my script would look like
I’m going to look at this as if the user is a constant user (name does not change)

' Set some objects
    Set WshShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

'get our constant user name from the system (change the UserNameHere)
Set colItems = objWMIService.ExecQuery ("Select * from Win32_UserAccount Where Name = 'UserNameHere'")	
	For Each objItem in colItems
	  objItem.Rename "NewUserNameHere"
	next
'change the user name
 WshShell.Run "cmd /c net user UserNameHere /fullname: NewUserNameHere", 1, true 

Kinda simple, but what happens if you don’t know the user name and simply want to change the user name?
I don’t think this is a good idea but lets say we just want to add some characters to the user…

So we are going to add a few lines and change a few lines…

' Set some objects
    Set WshShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
'set and get the current user account
    Set objNetwork = CreateObject("WScript.Network")
    strUserName = objNetwork.UserName

'get our constant user name from the system remember we got the current user before

Set colItems = objWMIService.ExecQuery ("Select * from Win32_UserAccount Where Name = '" & strUserName & "'")	
	For Each objItem in colItems
	  objItem.Rename strUserName & "Tech"
	next
'change the user name
 WshShell.Run "cmd /c net user "& strUserName &" /fullname: """ & strUserName & "Tech""", 1, true 
MsgBox "cmd /c net user "& strUserName &" /fullname: " & strUserName & "Tech"

Don’t forget to add some protection and logging, and if you need to change your auto login etc…
As well you might not need the MsgBox and you will need to reboot.

This entry was posted in Development, Scripting and tagged , , , . Bookmark the permalink.