Windows 7 - VBS Script to change last logged on user
Solution 1:
Thank you to those that helped with this issue.
After some further research I found that Windows 7 does not use the same 'Winlogon' registry key for storing the last loggedon user, insead it uses
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\LastLoggedOnUser
The second issue I had was allowing making the VBS script run as an elivated user (Administrator) without having to use 'runas' or an elevated command prompt.
After some trial and error I created the following script which successfully changes the last logged on user for domain added Windows 7 workstations.
Hopefully others here will find it helpful - I know I will!
Set Last Loggedon - Win7.vbs
'--------------
'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
Full Source: Windows 7 - VBS script to change last user logged on user
Solution 2:
I'm guessing that Windows 7 is configuring the registry sometime after you run the script. Likely when you log out. You could do something like storing the username in the registry under a custom key (HKLM\Software\MyCompany
is pretty common). Then you make a Shutdown or Startup script which does:
- Read the username from a value in the custom key. If it's blank, do nothing and quit.
- Set the
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WINLOGON\
values to use this username. - Blank the username value in the custom registry key.
It depends on when Windows is populating that registry key and how it's determining the last logged on user. You can use remote registry (either regedit or reg.exe
) to determine the data in the WinLogon values.