Tuesday 15 March 2011

Changing the Last Logged On User with a Batch File

During the day to day activities of a support desk its common to log into users PC's as administrator- the problem is that the computer remembers the last logged on user, and many users do not then notice and attempt to log in, or even worse- don't know what their own username is!

The fix is a simple registry hack- but this can be bothersome when your doing it on an hourly basis... So I have created a wee batch file to do the job for me.

I'll run though all of the steps required at a basic level so if you know what your doing you can skip the basics and if you're really lazy the full script is available at the bottom.

One thing to note is that XP and Windows 7 store this information in a different location in the registry, so the first thing I have to do is find out which one I'm working with and split my batch file up accordingly.

Here is a check list of what I want the script to do-

1. Prompt the user for the operating system version.

2. Display a list of usernames

3. Prompt the user for the name they wish to set as the last logged on user.

4. Create a registry file with that user.

5. Execute the registry file.

6. Delete the registry file.

First I need to create a set command to allow the user a choice of operating systems-

set /p choice=Select Operating System:

if '%choice%' == '1' goto WindowsXP
if '%choice%' == '2' goto Windows7

Now, displaying a list of usernames is much harder- the best I can do is to display the folder contents of 'documents and settings' or 'users'. Every user that logs on creates a folder in here with their username as the title so the folders in here should be a definitive list of everyone who has ever logged onto this computer- should is the operative word here, because due to a number of situations it may not be accurate so its more of a handy guide than anything-

Windows XP
ECHO List of Users:
cd\documents and settings
dir /w
Windows 7
ECHO List of Users:
cd\users
dir /w
I can now prompt the user for the name using a 'set' command-
set /p username=Please enter the name you want to appear as last logged on:
I now need to create a .reg file consisting of three lines of text- a title line, a registry location, and a key containing the name of the user. Another thing to note is that XP and Windows 7 store this information in a different location in the registry-

Windows XP
ECHO Windows Registry Editor Version 5.00 >> change.reg
ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] >> change.reg
ECHO "DefaultUserName"="%username%" >> change.reg
Windows 7

Windows 7 is a little more complicated, as it also requires a domain, so we also have to prompt the user for the domain-
set /p domain=Please enter the domain name:
Another important thing to remember is since the syntax is domain\username I have to double post backslashes on registry entries for a single on to appear!
:Windows7 
ECHO Windows Registry Editor Version 5.00 >> change.reg  
ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI] >> change.reg 
ECHO "LastLoggedOnUser"="%domain%\\%username%" >> change.reg

So, now I've got my registry file I need to execute it and then delete it-
C:\WINDOWS\regedit.exe /s change.reg 
DEL change.reg
All done- just like that!



Here is the script in full with plenty of comments, the comments are in red and REM'ed (which stops them being executed as lines of code)-


REM Stop the 'workings' of the script from showing up on the screen
@echo off

REM Clear the path just in case a command is too long to be executed
cd\

REM Put a wee menu on the screen to allow the user to see their choices
echo _____Select Operating System_____
REM The command 'echo:' just leaves a space
echo:
echo 1. Windows XP
echo 2. Windows 7
echo:

REM This creates a variable called 'choice' and prompts the user to enter it in
set /p choice=Select Operating System:

REM Once the user enters their choice the if statements fire off a 'goto' command which jumps the script to the required destination
if '%choice%' == '1' goto WindowsXP
if '%choice%' == '2' goto Windows7

REM This is the other end of the goto command, note that the destination is exactly the same as the goto command above, we want to put all of our windows XP stuff underneath this line
:WindowsXP

ECHO List of Users:

REM Go to the documents and settings folder and print out the contents
cd\documents and settings
dir /w
ECHO:

REM Create a variable called username and prompt the user to enter one in
set /p username=Please enter the name you want to appear as last logged on:


REM These lines enter the following text into a text document called change.reg
ECHO Windows Registry Editor Version 5.00 >> change.reg
ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] >> change.reg
REM Using the '%' symbol I insert the username variable that I set above
ECHO "DefaultUserName"="%username%" >> change.reg

REM this command is essential as after the windows XP code is executed I don't want it to then execute all of the windows 7 stuff as well, so I create a :End location below it and tell the code to go there now
goto End

REM this is the other end of the second choice goto at the top
:Windows7

cd\users
dir /w
ECHO:

REM I have to prompt for a domain as well so I create a new variable called domain
set /p domain=Please enter the domain name:
ECHO:
set /p username=Please enter the name you want to appear as last logged on:

ECHO Windows Registry Editor Version 5.00 >> change.reg
ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI] >> change.reg
REM Now I stick the domain and the username together with a backslash in between, I have to put two backslashes for a single one to appear in the text file
ECHO "LastLoggedOnUser"="%domain%\\%username%" >> change.reg

REM remember at the bottom of the XP commands we stuck in another goto command? Well here is where it comes out
:End

REM now I am executing 'regedit' the registry editor and telling it to silently import the file called change.reg that I created above
C:\WINDOWS\regedit.exe /s change.reg
REM I then delete the file to avoid clutter on the machine
DEL change.reg

REM its always good to bounce something back to the user just so they know the script has done something, even if it doesn't know if it has done something!
ECHO Username set to %username%

REM the last command just pauses the script until the user presses a key so the script doesn't just disappear
pause


If your lazy and you just want simple code that works here it is without the comments-


@echo off

cd\

echo _____Select Operating System_____
echo:
echo 1. Windows XP
echo 2. Windows 7
echo:

set /p choice=Select Operating System:

if '%choice%' == '1' goto WindowsXP
if '%choice%' == '2' goto Windows7

:WindowsXP

ECHO List of Users:

cd\documents and settings
dir /w
ECHO:

set /p username=Please enter the name you want to appear as last logged on:

ECHO Windows Registry Editor Version 5.00 >> change.reg
ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] >> change.reg
ECHO "DefaultUserName"="%username%" >> change.reg

goto End

:Windows7

cd\users
dir /w
ECHO:

set /p domain=Please enter the domain name:
ECHO:
set /p username=Please enter the name you want to appear as last logged on:

ECHO Windows Registry Editor Version 5.00 >> change.reg
ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI] >> change.reg
ECHO "LastLoggedOnUser"="%domain%\\%username%" >> change.reg

:End

C:\WINDOWS\regedit.exe /s change.reg
DEL change.reg

ECHO Username set to %username%

pause

No comments:

Post a Comment