Sometimes I need to log on to a user's computer to fix a problem, and I log on with my Domain Admins account so that I have administrator access. The problem is that after I log off, my Domain Admins account's name is still listed in the logon dialog box. Some users don't look at the username text box and repeatedly try to log on with my Domain Admins account, causing it to lock out because of bad password attempts. The users end up calling the Help desk to log on. How can I reset the username text box so that it displays the user's name?
The username text box in the logon dialog box is stored in the DefaultUserName value in the HKEY_LOCAL_MACHINE\SOFWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon registry subkey. To configure the username that's displayed in the logon dialog box, you simply need to set the DefaultUserName value to the user's username. I wrote LastLogonName.vbs, which Listing 3 shows, to automate this process. (Note that if you work in an environment that uses the Novell client, this script won't work because the last logged-on username displayed at the logon screen is stored in a different registry key than the one above.) The script uses the following command-line syntax:
Specify the username you want to use on the scriptâ€™s command line. Because itâ€™s also useful to set the DefaultUserName value on a remote computer, the script also accepts the /C option with a computer name to set the DefaultUserName value for a remote computer.
LastLogonName.vbs uses the first unnamed argument (i.e., the first command-line argument that doesn't begin with a / character) as the new default username, as the code at callout A in Listing 3 shows. Callout B shows how the script reads the /C option. If the /C option exists on the command line, the script stores the argument in the ComputerName variable; otherwise, the script uses a dot as the computer name. A dot is Windows Management Instrumentation's (WMI's) shorthand for the local computer.
Next, LastLogonName.vbs attempts to connect to the WMI StdRegProv provider on the computer, as callout C shows. The script uses the On Error Resume Next statement to disable VBScript's default error handler, then makes the WMI connection. If the WMI connection attempt raises an error (i.e., the Number property of the Err object is not zero), the script uses its Die subroutine to display the error message and exits.
If the WMI connection succeeds, the script uses the SetStringValue method of the StdRegProv object to assign the new username. If the SetStringValue method fails, it returns a non-zero value. Finally, the script checks the result of the SetStringValue method. If the result is nonzero, the script calls the Die subroutine; otherwise, it displays a message that the last logon username was updated.