Windows 7 – VBS script to change last user logged on user

The following VBS script will change the last user logged on user for a Windows 7 workstation.

This script has been used with Windows 7 workstation which is on an Active Directory domain.

MSWin7-LogonScreen1

Some background:

With Windows XP you used to be able to edit the registry information under ‘Winlogon’ (see: Using VBS to set the last user logged on (Windows XP))

However, Windows 7 no longer uses this registry information for the last logged on user, it now uses:

"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\LastLoggedonUser"

Additionally, this section of the registry requires elevated rights to modify, however if you run a VBS Script by double clicking it is not elevated.

To fix this, I was able to find a workaround to make the script run as an elevated user (see: Windows 7 – Run VBS script as elevated user (UAC))

Set Last Loggedon – Win7.vbs

To use the script, copy the following code into a new text file and save as ‘Set Last Loggedon – Win7.vbs’, run the script and enter in the required username. If it is a local uer account leave the domain blank.

Full Download: here

'--------------
'Start of UAC workaround code

If WScript.Arguments.length =0 Then
Set objShell = CreateObject("Shell.Application")

objShell.ShellExecute "wscript.exe", Chr(34) & _
WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else

'--------------
'Start of code

dim WSHShell
Set WSHShell = Wscript.CreateObject("WScript.Shell")
dim strRegKey
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
strRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"

StrUser = InputBox("Please enter in username," & vbCrLf & vbCrLf & "e.g. joe.local", "Set Last logged on", "UserName")
StrDomain = InputBox("Please enter in domain for logon," & vbCrLf & vbCrLf & "e.g. DOMAIN", "Set Logon Domain OR leave blank if a local user account")

If StrDomain = "" then
StrDomain = "."
Else
End If

wshShell.RegWrite strRegKey & "LastLoggedOnUser", StrDomain & "\" & StrUser, "REG_SZ"

WScript.Echo "Setup Completed. Please restart the computer to complete the process"

'--------------
'End of code

'--------------
'End of UAC workaround code

End If