Enterprise Software

Automatically run a batch file when you open a Windows XP command prompt

You probably run the same few commands each time you start using the command prompt in Windows XP. You can save yourself from typing any commands at all if you add the path and name of the batch file to a special key in the registry. Here's how to add them.

You probably run the same few commands each time you start using the command prompt in Windows XP. For example, perhaps you first switch to the root directory and then clear the screen. Then, you may have put these commands into a batch file and saved the file to the C:\Documents and Settings\{username} folder so that when you open the command prompt, you simply type the name of the batch file to issue the commands.

You can save yourself from typing any commands at all if you add the path and name of the batch file to a special key in the registry. Here's how to add them:

1. Launch the Registry Editor (Regedit.exe).

2. Go to the  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor key.

3. Double-click the AutoRun value to access the Edit String dialog box.

4. In the Value Data text box, type the path and name of the batch file. Be sure to enclose the text in double quotes -- for example, "C:\Documents and Settings\greg\go.bat".

5. Click OK to close the Edit String dialog box and close the Registry Editor.

Now, your batch file will automatically run every time you open the command prompt window.

Caution: Editing the registry is risky, so make sure you have a verified backup before making any changes. Note: This tip is for both Windows XP Home and Professional.

Miss a tip?

Check out the Windows XP archive, and get more hands-on Windows XP tips.

Stay on top of the latest XP tips and tricks with our free Windows XP newsletter, delivered each Thursday. Automatically sign up today!

About

Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

22 comments
mray
mray

I like to use batch files in XP for a number of things. I'm really lazy when it comes to typing. And I like things to happen quickly. 2 batch files I setup on almost every computer are x.bat and i.bat. x.bat just does this: @echo off exit Just saves my fingers from having to type exit all the time. i.bat does this: @echo off ipconfig It seems I'm always checking my IP address information since I have a lot of things going on on my computer. A couple others I like to add are iren.bat, irel.bat, and iall.bat, which are 3 different options with ipconfig /renew, /release, and /all for more detailed information. I'm surprised more people don't do the x.bat. So much easier than typing exit! Matt Ray MC Consulting, GroupWiseGuru.com mray@gwguru.com 661.885.2699

Ermyas A.
Ermyas A.

Thank you here is from a different angle. We can also execute a number of bat files from the desk top without going to the root directory. Let us say we have a bat file (simplebat.bat) that opens yahoo, SPSS10, Windows Media Player MSAccess, etc... . First Create a folder c:\Task1. Open a note pad and copy the following and save as simplebat.bat in the folder c:\Task1. @echo off cls :Mainmenu echo.--------------------------------------- echo 1.Open Yahoo echo 2.Open SPSS 10 echo 3.Open Windows media player echo 4.Open Microsoft Access echo 5.Open a Computer in a Net Work echo 6.Exit echo.--------------------------------------- echo. choice/C:123456/N Enter your choice number: break off cls echo. echo. echo. if errorlevel 6 goto Exit if errorlevel 5 goto net if errorlevel 4 goto MSAccess if errorlevel 3 goto WMPlayer if errorlevel 2 goto SPSS if errorlevel 1 goto Yahoo :Yahoo "c:\Program Files\Internet explorer\Iexplore.exe" cls goto Main echo. :SPSS "c:\Program Files\SPSS\spsswin.exe" cls goto Main echo. :WMPlayer "c:\Program Files\windows media player\wmplayer.exe" cls goto Main echo. :MSAccess "C:\Program Files\Microsoft Office\Office11\MSACCESS.EXE" cls goto Main echo. :net echo.--------------------------------------- echo TO VIEW/OPEN A NET WORK COMPUTER, echo COPY FOLDERS/FILES FROM SHARED echo FOLDERS TO YOUR COMPUTER echo. echo 1.View IP configuration echo 2.View shared folders echo 3.Open/Map a drive letter echo 4.Copy Folder(s)/Files to your computer echo 5.Open lpt1(shared printer) echo 6.Return echo.--------------------------------------- echo. choice/C:126/N Enter your choice number: break off cls echo. echo. echo. if errorlevel 6 goto return if errorlevel 2 goto view if errorlevel 1 goto ipconfig :ipconfig echo ipconfig /all goto net cls :view echo. echo. cls :return goto mainmenu :Exit Then we need to create a short cut for the command prompt, rename it to Task1 and set its properties as follows: 1.Create a short cut for the Command Prompt on your desk top. 2.Right click on Command Prompt and rename Command Prompt to Task1. 3.Right click on Task1 and Select properties. 4.Click the Shortcut tab in the properties window . 5.Type c:\Task1\simplebat.bat in the Target text box. 6.Type c:\Task1 in the Start in text box. 7.Double click Task1 Now we get the menu to execute the different daily tasks. This way it is possible to have different types of bat files on the desk top. NOTE: copy choice.com in c:\task1

techrepublicsucks
techrepublicsucks

Download and install PowerShell from Microsoft for free. It runs all CMD commands, several UNIX commands and its own PowerShell commands. On top of that it includes a powerful programming language. It runs on XP, Vista, 2003 and is a required core component of Exchange 2007. I programmed a menu-based command prompt interface that I use instead of CMD. CMD is history, lets all move forward.

Absolutely
Absolutely

I think you people are all afraid of the registry. Like, if you mistype one little letter, you'll magically turn your Windows computer into a doorstop, and it will never w

icmp30
icmp30

Make a new shortcut to cmd.exe and use the /k switch - eg %windir%\system32\cmd.exe /k c:\util\doswin.bat No editing the registry and you can have different commands run according to which shortcut you double-click on.

kpak44wh
kpak44wh

why? if you screw up the batch file, you might crash the whole system. We did a data migration contract were the programmer put a bunch of batch files on the recovery cd. It us weeks to fix the screwup. With anything you do, if you can't fix it, don't mess with it. That has saved my bacon more times than I can recall.

afgcons
afgcons

Sorry to disagree - IMHO the registry tweek (if used ad all) should be limited to general command setup, valid for all uses of cmd. Instead I made good exprience creating specialized Windows SHORTCUTS. This one brings up the "list" command with a file list of drive d: Target: C:\WINNT\system32\CMD.EXE /c list d: Here, C:\WINNT\system32\CMD.EXE /c \\home\s\home.bat W2K AFG is the target in a shortcut in startup. When Windos boots this copies the hosts file, issues net commands and the like. The 1st example runs a single command. The 2nd one a bat file on a server.

Lightning Joe
Lightning Joe

If your batch file runs a menu shell, you're back in DOS Heaven!

mike_h
mike_h

I find it easier to use the /K command line switch in the shortcut that starts the command. That way, I can pick a different batch file for each short cut. To do this, make a copy of the standard shortcut from the Start Menu > Programs > Accessories menu; then right-click on the copy, click "Properties. The Target field will probably be something like: "%windir%\system32\cmd.exe", which can be changed to something equally like "%windir%\system32\cmd.exe /K C:\Work\JavaDev\JavaSetup.bat" If you use other command line options, the /K option must come last because the rest of the command line that calls cmd.exe is treated as the initial command to execute. Lots of additional information can be had by entering "cmd /?" at the command prompt.

moose812
moose812

I use .bat files to check connectivity (ping) and run tracert to various locations

rapell
rapell

especially when you use the command prompt for so many other tasks. Does the batch run and close the shell or it runs and leaves one with a prompt to perform other tasks?

Grayson Peddie
Grayson Peddie

Try show an illiterate user how to edit a registry, and s/he can wonder around and play with different settings, but then the computer won't work anymore until the factory defaults have been restored to an original clean configuration. :) Nope! Sorry!

techwriter
techwriter

not that I'll use it often, but it's one more handy thing. . .

mike_h
mike_h

...is "/?". Most executables meant to be called from the CMD command line, and most CMD builtin commands, will produce pretty good self-help documetnation when called with the "/?" option. (Also, the "HELP cmd" command works with the standard Windows commands.) I mention this because I pointed out earlier, and icmp30@... later, that /K is the option to use (not /C) to run commands when starting an interactive shell. The help text from "CMD /?" points out the difference. There are other goodies buried in that help text. One nice option is "/D", which will *prevent* execution of AutoRun commands from the registry. I don't use it, but this seems like one way to keep registry-borne trojans out of CMD sessions.

Absolutely
Absolutely

just when starting the Command Prompt. Thanks for your tips, though.

Absolutely
Absolutely

For people who always, or almost always, begin their use of the Command Prompt with the same keystrokes, this will save them hours of work during their careers, a few seconds at a time. It's intended to leave the shell open, so not really limiting at all.

Senior Program Analyst
Senior Program Analyst

if you add items to the batch file which you seldom need or specific to one use only then yes that may be limiting. If you use (or it doesnt hurt anything to be used) on most all occassions then it could be benefitial. some possibles (and there are many others) of things some people repeat often: prefer to start out in a special directory such as the C Drive Root; You like seeing particular items in your Prompt such as the time; You prefer a specific printer on the network - why wait until you need it to set it up? Anything you prefer to do or work with. These type things don't limit you.

razvandudu
razvandudu

Yes, it does leave the prompt open. Consider the following situation: you always type --------------------------------------- net use X: \\myserver\myhomedir * /USER:mylogin X: CD "X:\My little data processing apps\" --------------------------------------- when you open the command prompt to run some apps... (that's just an example) and you do it quite often. Then you save those commands in a batch file (or .cmd one to be "legacy-free") and put the name and path of that file in the registry key. That way every time you open the command prompt you will be prompted for the password for the connection to \\myserver and then all the following commands will be executed as if you were typing them yourself, leaving you with the prompt open in "X:\My little data processing apps\" ready to execute your apps. That trick makes sense if you have at least two or three long commands to type EVERY time you open a command prompt... otherwise it can become a nuisance.

pennatomcat
pennatomcat

include a dos-like menu system pointing to additional batch files, including a dos prompt.

kpak44wh
kpak44wh

You can do anything you want to do to the registry, but if you do on it a customer's computer and mess it up, it's going to be a pain. Especially if he is watching you work. My motto is K.I.S.S. Don't mess with anything you can't fix, or you haven't tried on your own computer first.

a.southern
a.southern

So where do we put the himem.sys command?

Editor's Picks