Tech Tip: Run commands automatically/Control console command extensions

Windows 2000 Professional: Run commands automatically

Windows provides a handful of means to control the way console sessions start and function. For example, you can click a console's Control menu, select Properties, and configure properties such as cursor size, display options, font, and layout.

Besides configuring properties, you might also want to execute commands automatically when you open a console. For example, you might want to load a device driver, switch to a particular directory, set the command prompt, or execute a batch file.

Adding a command string to the registry is one simple solution. Open the Registry Editor to the following key and enter a command string as its value:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\Autorun

Cmd.exe executes this command string at startup for all command console sessions.

You can include a command string for a specific console session by starting the session with the /K switch. For example, the following example would start a console session and change to the \Documents and Settings folder:

Cmd.exe /K "CD \Documents and Settings"

Note: Editing the registry is risky. Before making any changes, back up the registry so you can restore it if something goes wrong.

Windows 2000 Server: Control console command extensions

Cmd.exe supports command extensions, which provide changes or enhancements to the following commands:


The effects on each command are specific to that command. For example, if command extensions are on, the MD command creates any required intermediate directories. If you issued the command MD \DIR1\DIR2\DIR3 when neither DIR1 nor DIR2 existed, MD would create DIR1 and DIR2, as well as DIR3. The PROMPT command supports the special prompt codes $+ and $M. When command extensions are enabled, the SET command supports expandable variablesincluding %CD%, %DATE%, %TIME%, %RANDOM%, %ERRORLEVEL%, %CMDEXTVERSION%, and %CMDCMDLINE%.

These command extensions are enabled by default. In some cases, such as if you want to prevent users from working with some of the additional options, you might prefer to turn off extensions.

You can turn off extensions for a single instance of Cmd.exe by adding the /E:OFF switch to the Cmd.exe command string. To turn off extensions for all instances of the command console, set the value of the following key to 0:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\EnableExtensions

You can also control extensions on a per user basis by setting the value of this key to 0:

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions

Note: Editing the registry is risky. Before making any changes, back up the registry so you can restore it if something goes wrong.

Editor's Picks

Free Newsletters, In your Inbox