Customize the Windows Command Shell
Streamline your command-line efforts
April 10, 2007
Microsoft OSs have used a command-line interface ever since the company released its first version of DOS. In MS-DOS (and even in Windows Me, Windows 98, and Windows 95), the primary command shell was command.com. Starting with Windows NT, Microsoft provided a command shell called cmd.exe. Cmd.exe is a Windows console program, which means that it uses a text-based window (technically known as a console window), as opposed to a graphical window, for its input and output. A cmd.exe console window looks similar to an MS-DOS command prompt, but the resemblance is merely superficial. Cmd.exe is much more powerful and more customizable than its MS-DOS predecessor.
You can customize the command shell in one of two main ways:
The console window—Windows provides a dialog box with four tabs that let you configure cmd.exe's console window in various ways. Most of these changes aren’t specific to cmd.exe but instead relate to console programs in general.
The cmd.exe command—Cmd.exe provides some handy features that you can customize to suit your preferences. These features make working from the command line easier than ever.
Customizing the Console Window
Windows provides a tabbed dialog box that gives you several options for customizing the command shell console window's appearance and behavior. To access this dialog box, right-click the command window’s title bar and select Properties. The dialog box that opens contains four tabs: Options, Font, Layout, and Colors. (If you right-click the cmd.exeshortcut icon and select Properties, Windows adds shortcut-related tabs to the dialog box, but I won't discuss those here.) The Font and Colors tabs are self-explanatory, but the Options and Layout tabs require some clarification.
Options. The Options tab, which Figure 1 shows, has four sections: Cursor Size, Display Options, Command History, and Edit Options. The Cursor Size section lets you choose the size of the blinking cursor. The Display Options section lets you select whether the console window starts in window or full-screen mode. To toggle between window and full-screen mode while working in a console window, press Alt+Enter.
The Command History section configures the behavior of the console window's command history. (I describe how to use the command history in a later section.) The Buffer Size option defines the number of commands the command history will store, and the Number of Buffers option determines how many console programs can have a separate command history. (For example, Telnet and FTP, both console programs, can have their own command histories.) The Discard Old Duplicates option determines whether the command history retains or removes duplicate commands.
The Edit Options section has two options: QuickEdit Mode and Insert Mode. QuickEdit is enabled by default in Windows 2000 but not in Windows XP and later. When QuickEdit is enabled, left-clicking and dragging in the console window selects text to copy to the clipboard; press Enter to copy the text. Right-clicking automatically pastes the clipboard's contents into the command window. When QuickEdit isn’t enabled, mouse clicks pass to the program runningin the console window. To copy and paste when QuickEdit isn’t enabled, right-click in the console window and select Mark to copy or Paste to paste. The Insert Mode check box determines the initial state of the insert mode when you type at the command line. To toggle the insert mode, press the Insert key at the command line.
Layout. The Layout tab, which Figure 2 shows, contains three sections: Screen Buffer Size, Window Size, and Window Position. The Screen Buffer Size width and height define the number of columns and rows in the console window's screen buffer. The Window Size width and height define how much of the screen buffer is viewable. Scroll bars appear if the Screen Buffer Size width and/or height exceed the corresponding Window Size settings. By default, the screen buffer size is 80 characters wide by 300 lines long, and the window size is 80 by 25; a vertical scroll bar lets you view previous output that has scrolled past the top line of the window. The Window Position section lets you decide whether you always want the console window to appear in a specific location, or if you want to let the system position the window for you.
When you’re finished customizing the console window, click OK to save your changes.
Windows will ask whether you want to apply the changes to the current window only, or if you want to modify the properties for the shortcut that opened the window. Select your preference and click OK.
Setting console window defaults. If you want to configure the default settings for all console windows, right-click a console window's title bar and select Defaults, as Figure 3 shows. The four-tabbed dialog box in Figure 1 will appear, but it will be titled Console Windows Properties, indicating that the changes you make will apply to all console windows, not just the current window. On XP and later, the Edit Options section contains an additional choice called AutoComplete. The AutoComplete option, if enabled, lets you use the Tab key to automatically enter file and directory names at the command line. Behind the scenes, this check box sets a registry value. (You can change this registry value to enable the AutoComplete behavior in Win2K or NT, although the setting isn’t accessible from the GUI. In a later section, I discuss the registry value you need to use.)
Customizing Cmd.exe
In addition to having a customizable console window, the cmd.exe command has some handy features of its own. Filename and directory name completion, line editing and command history, AutoRun scripts, and macros all make command line administration easier.
Filename and directory name completion. When this feature is enabled, you can use a customizable control key (often the Tab key) to cycle through filenames and directory names when typing at the command line. Cmd.exe actually provides two completion behaviors: filename completion, which cycles through filenames and directory names, and directory name completion, which cycles through only directory names. To enable filename completion, use any of the following methods:
Open the console window’s Properties dialog box, as I discussed in the previous section, and select the AutoComplete check box. (This check box appears only on XP and later.) Enabling this feature lets you use the Tab key to perform filename completion.
Modify the shortcut to cmd.exe and add the /f:on option to the command line. This action lets you use Ctrl+F to enable filename completion and Ctrl+D to enable directory name completion.
Modify the CompletionChar and/or PathCompletionChar registry values. These values are located in the HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor and HKEY_LOCAL_MACHINESOFTWAREMicrosoftCommand Processor registry subkeys. (Note that both registry values can exist in both subkeys, for a total of four possible values.)
Both CompletionChar and PathCompletionChar are REG_DWORD values that you can set to the value of a control character that you want to use for filename completion or directory name completion. You set the REG_DWORD value to represent the Ctrl key you want to use (e.g., 1 represents Ctrl+A, 2 represents Ctrl+B, and so on). To disable a completion behavior, set its value to 32 (i.e., a space—which isn’t a valid control character). The values in HKEY_LOCAL_MACHINE apply to everyone who uses that computer. If you define HKEY_CURRENT_USER values, these values override the HKEY_LOCAL_MACHINE settings. In addition, the /f:on option overrides the registry values in both HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER. Note that Regedit uses hexadecimal as a default base when creating or editing REG_DWORD values, so be sure to change the base to decimal (or enter it as a hex number).
Enabling the AutoComplete check box in the console window’s Properties dialog box on XP or later is equivalent to configuring the CompletionChar value in HKEY_USERS to 9 (i.e., Ctrl+I or the Tab key). If you clear the AutoComplete check box, Windows sets the value to 32 to disable filename completion.
To use filename or directory completion, simply press the configured control character at the command line. To narrow your search, enter the first few characters of the filename or directory name you want to search for. For example, suppose you've configured Tab (i.e., 9) as your filename completion character and Ctrl+D (i.e., 4) as your directory name completion character. If you wanted to display the C:boot.ini file, you’d enter
type C:boo
In this example, the Tab keystroke will complete the filename (e.g., boot.ini). If other files exist that start with the letters boo, the Tab key will cycle through them. Another example is if you enter
cd Pro
In this case, the Ctrl+D keystroke will complete the directory name (e.g., "C:Program Files"). Both completion commands automatically insert double quotes if filenames or directory names contain spaces. If other directory names exist that start with the letters Pro, Ctrl+D will cycle through them. Note that if you enable filename completion, the Tab key will cycle through both filenames and directory names, but you can still configure the PathCompletionChar value separately.
You can also navigate to deeply-nested subdirectories by appending a backslash after each set of directory name plus completion command. For example, suppose you wanted to navigate to the My Documents directory in the Administrator profile. You could enter
cd DocAdmMy
Cmd.exe would automatically insert double quotes around the path, so that the completed command would look like
cd "Documents and SettingsAdministratorMy Documents"
Note that both filename and directory name completion delete the remainder of the command line. You need to keep this behavior in mind if you want to use filename or directory name completion to complete a name at the beginning or in the middle of a command.
Line editing and command history. Cmd.exe also provides a group of convenient command keystrokes that let you easily recall and edit commands in the command window. The command history stores previously entered commands so that you can easily recall previous commands for editing and reentry. Table 1 lists the keystrokes you can use.
The Up Arrow and Down Arrow keys navigate through the commands in the command history. Commands are stored in the order in which you enter them, so the oldest command is at the top of the list. For example, to select the second-oldest command in the command history, press Page Up, then the Down Arrow. To set the command history size, select the Options tab in the console window’s Properties dialog box and set the Buffer Size option in the Command History section (see Figure 1).
The F7 key displays a list of commands in the command window, by number. To select a command to run again, use the arrow keys followed by Enter. If you don't want to run the command, press Esc. If you want to edit the command first, press the Left Arrow or Right Arrow key.
To search through previous commands in the command history, type the first few characters of the command and press F8. The first most recent match from the command history will display. Press F8 to go to the next most recent match, and so forth. For example, typing
cd
finds the first most recent command that starts with the letters cd followed by a space; pressing F8 again finds the next most recent cd command, and so on.
AutoRun scripts.An AutoRun script is a shell script (typically with a .cmd or .bat extension) that runs when you start cmd.exe. To enable an AutoRun script, create a REG_SZ or REG_EXPAND_SZ registry value in the HKEY_LOCAL_MACHINESOFTWAREMicrosoftCommand Processor subkey and/or the HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor subkey. In the AutoRun value, specify the name of the script you want to run. The script that you specify in HKEY_LOCAL_MACHINE will run first, followed by the script that you specify in HKEY_CURRENT_USER. If any part of the script's path or filename contains spaces, enclose the entire path and filename in quotes. Figure 4 shows an example configuration that runs the AutoRun.cmd script for all users on the computer. If you want to bypass the AutoRun scripts, add the /d option to the cmd.exe command line.
Macros.Macros are a form of command substitution that let you create commands without a script. Although macros aren't as powerful as shell scripts, they do support parameter substitution and multiple commands. The Doskey program provides a set of commands for managing various aspects of the console window's command-line interface, including macros. (I won’t bore you with the historical reasons for this convention.)
To create a macro, use the command
doskey =
where macroname is the macro's command name (i.e., the command name you'll type to run the macro), and text is the command that the macro will carry out. For example, suppose you frequently navigate to your user profile directory. Enter the following command at the command line:
doskey home=cd /d "%USERPROFILE%"
After you create this macro, you can simply type home to navigate to your user profile directory. To delete a macro definition, use the following command:
doskey =
You can also read a list of macros from a text file. In the text file, add one macro definition to each line of the file (but don't include the Doskey keyword). You can then use the following Doskey command to read the file:
doskey /macrofile=
where filename is the text file that contains the macro definitions. If the name contains spaces, enclose it in quotes.
You can use the Doskey /macroscommand to display a list of currently defined macros. You can redirect this output to a text file that's suitable for the Doskey /macrofile command. For example, the following command will create the text file C:scriptsmacros.txt:
doskey /macros > C:scriptsmacros.txt
You can then use the Doskey /macrofile command in an AutoRun script to make the macros automatically available every time you open a command shell. You can also easily edit this file to modify your macros.
Macros support a list of special character sequences that extend their functionality. Table 2 lists special characters you can use in macros. Consider the following macro definition:
tsc=mstsc /v:$1 /console
This macro starts a remote desktop connection to the specified computer’s console session. For example, to connect to the console on a server named mercury, enter
tsc mercury
The macro will replace $1 with the command-line argument (i.e., mercury), so the command that actually runs is
mstsc /v:mercury /console
If you need a macro to have access to the entire command line, use $*. For example:
np=notepad $*
All of this macro's command-line parameters pass unchanged to Notepad.
You can use the $T sequence to separate multiple commands. For example:
todo=cd /d "%USERPROFILE%My Documents" $T notepad todo.txt
This macro navigatesto your My Documents directory and opens the todo.txt file in Notepad. The $T sequence separates the two commands.
Master the Command Shell
Many systems administrators are comfortable working with GUI tools but aren’t used to working at the command prompt. The Windows command shell is an important and useful tool in a systems administrator's arsenal. The tips and techniques I provide will help you master the command shell so that you can customize it for your environment to streamline your command-line efforts for improved efficiency and productivity.
About the Author
You May Also Like