Over the last few days at my new job I’ve encountered a problem.
Every time a user logs in, there are programs that run. The problem is that you only want some programs to run one time.
Whenever you login to a few key systems, the startup items would kick off automatically. No matter if they had already been started on another session or not.
So, I thought to myself. Will, you need to check for a few things:
I wrote a Windows Batch script to take care of this for us. Let’s take a look at the script:
@echo off
FOR /F "tokens=3" %%a IN ('netsh diag show computer /v ^| FIND /i "username"') DO SET username=%%a
echo The user is %username%
IF /I "%username%"=="DOMAIN\username" (
ECHO User is logged in. Proceed with Autostart.
ECHO Checking for running processes.
pv.exe -d10000
pv.exe > running_process.txt
FIND "NOTEPAD.EXE" running_process.txt
IF ERRORLEVEL 1 START c:/windows/system32/NOTEPAD.EXE
ECHO All Done. EXITING.
cls
)
In this example, I’m checking for notepad.exe to already be running and if it’s not then we will automatically start it.
You will need to download, and place pv.exe in the same folder as you will save the .bat file we will create here. I’ll post the source files for the example at the end of the tutorial.
Now, let’s take a look at the code:
@echo off
FOR /F "tokens=3" %%a IN ('netsh diag show computer /v ^| FIND /i "username"') DO SET username=%%a
Line 1 says to turn echo off. Turning echo off is an important step if you do not want your users to watch every step of the script as it runs.
Line 2 is running a command to find the current username logged into the system – it then sets the username into a variable to be used through the remainder of the script.
echo The user is %username%
Line 4 Sends a line to the console displaying the current username in the command prompt window.
IF /I "%username%"=="DOMAIN\username" (
Along with line 2, line 6 is the meat and potatoes of this script. This is checking to see if the current username (that we got from line 2) meets what you want the username to be. Then, if it is – it will allow for the script to enter inside of the ( and execute the code.
ECHO User is logged in. Proceed with Autostart.
ECHO Checking for running processes.
pv.exe -d10000
pv.exe > running_process.txt
FIND "NOTEPAD.EXE" running_process.txt
IF ERRORLEVEL 1 START c:/windows/system32/NOTEPAD.EXE
ECHO All Done. EXITING.
cls
Lines 8-10 are displaying more lines to the console to display a status to the user.
Lines 11-12 are running the PV.EXE that you downloaded, this this does is create a list of running processes and the > character takes all of the results of that window and sends it to the running_process.txt file.
You may have noticed the -d10000 after the first invocation of pv.exe, this delays the script from executing the next command so that pv.exe can startup and then pipe the content.
Line 14, does a text search inside of the running_process.txt file to locate the text string “notepad.exe”. Line 15, takes the result of that FIND command, 0 if it’s there, 1 if it’s not. ERRORLEVEL 1 would then START the command that you would like to autostart. In our example, this starts notepad.
You can check for multiple processes, and start them, simply copy and paste lines 14-15 and rename the process you want to look for and start.
For example:
ECHO User is logged in. Proceed with Autostart.
FIND "NOTEPAD.EXE" running_process.txt
IF ERRORLEVEL 1 START c:/windows/system32/notepad.exe
FIND "CALC.EXE" running_process.txt
IF ERRORLEVEL 1 START c:/windows/system32/calc.exe
This will look for calculator and notepad, and launch if they are not running.
Line 17 Tells the user that it’s done.
Line 18 clears the console window.
So, how do we use this script?
Note: the process the script checks for is case sensitive. So, under task manager you have a process that is running as SomeThing.EXE, you will need to put that in exactly as it is displayed.