Showing posts with label Common Statements. Show all posts
Showing posts with label Common Statements. Show all posts

Saturday, 28 June 2014

Getting Acquainted with Variables in CMD

Getting Acquainted with Variables

In command-line scripting, what we commonly call variables are more properly called environment variables. Environment variables can come from many sources. Some variables are built into the operating system or derived from the system hardware during startup. These variables, called built-in system variables, are available to all Windows processes regardless of whether anyone is logged on interactively. System variables can also come from the Windows Registry. Other variables are set during logon and are called built-in user variables. The built-in user variables available are the same, no matter who is logged on to the computer. As you might expect, they are valid only during an actual logon session, that is, when a user is logged on.

You can see a listing of all the variables known in the current instance of the command shell by typing set at the prompt. In addition to the normal system and user variables, you can create variables whenever Windows is running, which is exactly what you’ll do when you program in the command shell. You define variables for the current instance of the command shell using the SET command and the following syntax:
set variable_name=variable_value
such as
set working=C:\Work\Data
set value=5
set string="Hello World"
Some variables, including system and user environment variables, have special meaning in the command shell. These variables include path, computername, homedrive, and many other important environment variables. One environment variable that you should learn more about is errorlevel, which tracks the exit code of the most recently used command. If the command executes normally, the error level is zero (0). If an error occurs while executing the command, the error level is set to an appropriate nonzero value. Error values include
  • 1  Indicates a general error
  • 2  Indicates an execution error, meaning the command failed to execute properly
  • 2  Indicates a math error, such as when you create a number that is too large for the command shell to handle
You can work with the errorlevel variable in several ways. You can check for a specific error condition, such as
if "%ERRORLEVEL%"=="2" echo "An error occurred!"
Or, you can use the following special syntax and check for a condition equal to or greater than the specified exit code:
if errorlevel 2 echo "An error occurred!"
Note 
You’ll see more on errorlevel and if statements later in the chapter in the section titled “Using Conditional Statements.”
When you are finished working with variables, it’s good form to dispose of them. You do this to free memory used by the variable and prevent problems or unexpected results if you accidentally refer to the variable in the future. To clear out a variable, you simply set the variable equal to nothing, such as
set working=
Now the variable is cleared out of memory and is no longer available.

Common Statements and Commands for Scripts in Windows CMD

Common Statements and Commands for Scripts

So far in this book, I’ve discussed commands but haven’t really introduced what a statement is. While these terms are often used interchangeably, the term statement technically refers to the keyword for a command, such as the rem statement, but it can also refer to a line of code that includes all the command text on that line. In some programming languages, such as Java, each statement must be terminated with a specific character. With Java, the terminator is a semicolon. The command line doesn’t look for a specific terminator, other than the end of the line, which is assumed when the command interpreter reads any of the following:
  • Line break (such as when you press Shift+Enter)
  • Carriage return and line break (such as when you press Enter)
  • End-of-file marker
Now that we’ve discussed how to create scripts, let’s look at common statements and commands you’ll use in scripts, including
  • Cls  Clears the console window and resets the screen buffer
  • Rem  Creates comments in scripts
  • Echo  Displays messages at the command line and turns command echoing on or off
  • @  Controls command echo on a line-by-line basis
  • Title  Sets the title for the command shell window
  • Color  Sets the text and background colors used in the command shell window

Clearing the Command-Shell Window

Clearing the command-shell window before writing script output is usually a good idea. You clear the command-shell window using the CLS command. Why not try it? At the command line, type cls and press Enter. The console window clears and the cursor is positioned in the top left corner of the window. All the text in the screen buffer is cleared as well.
You could add the CLS command to the sample script listed previously, as shown in this example:
cls
hostname
ver
ipconfig -all

Adding Comments to Scripts

You use the rem statement to add comments to your scripts. Every script you create should have comments detailing
  • When the script was created and last modified
  • Who created the script
  • What the script is used for
  • How to contact the script creator
  • Whether and where script output is stored
Not only are the answers to these Who, What, How, Where, and When questions important for ensuring that the scripts you create can be used by other administrators, they can also help you remember what a particular script does, especially if weeks or months have passed since you last worked with the script. An example of a script that uses comments to answer these questions is shown as Listing 3-2.

Listing 3-2: Updated Sample Script with Comments
Start example
rem ************************
rem Script: SystemInfo.bat
rem Creation Date: 2/2/2004
rem Last Modified: 3/15/2004
rem Author: William R. Stanek
rem E-mail: williamstanek@aol.com
rem ************************
rem Description: Displays system configuration information
rem including system name, IP configuration
rem and Windows version.
rem ************************
rem Files: Stores output in c:\current-sys.txt.
rem ************************
hostname > c:\current-sys.txt
ver >> c:\current-sys.txt
ipconfig -all >> c:\current-sys.txt
End example
Later in this chapter, in the section titled “Passing Arguments to Scripts,” I’ll show you how to use your comments as automated help documentation. Before we get to that, however, keep in mind that rem statements can also be used to
  • Insert explanatory text within scripts, such as documentation on how a procedure works.
  • Prevent a command from executing. On the command line, add rem before the command to comment it out.
  • Hide part of a line from interpretation. Add rem within a line to block interpretation of everything that follows the rem statement.

Managing Text Display and Command Echoing

The echo command has two purposes. You use the ECHO command to write text to the output, which can be the command shell or a text file. You also use the ECHO command to turn command echoing on or off. Normally, when you execute commands in a script, the commands as well as the resulting output of the command are displayed in the console window. This is called command echoing.
To use the ECHO command to display text, enter echo followed by the text to display, such as
echo The system host name is:
hostname
To use ECHO to control command echoing, type echo off or echo on as appropriate, such as
echo off
echo The system host name is:
hostname
Use output redirection to send output to a file rather than the command shell, as follows:
echo off
echo The system host name is: > current.txt
hostname >> current.txt
To experiment with suppressing command echoing, start a command shell, type echo off, and then enter other commands. You’ll find that the command prompt is no longer displayed. Instead, you see only what you type into the console window and the resulting output from the commands you’ve entered. In scripts, the ECHO OFF command turns off command echoing as well as the command prompt. By adding the command ECHO OFF to your scripts, you keep the command-shell window or the output file from getting cluttered with commands when all you care about is the output from those commands.
Tip 
By the way, if you want to determine whether command echoing is enabled or disabled, type the ECHO command by itself. Give it a try. If command echoing is on, you’ll see the message Echo Is On. Otherwise, you’ll see the message Echo Is Off. Experiment with the ECHO OFF command in your scripts and you may detect a bit of a problem here. If the ECHO OFF command turns off command echoing, how do you prevent the ECHO OFF command itself from echoing? Don’t worry; that’s discussed in the next section.
Real World 
One question that other command-line programmers frequently ask me is, How do you get a blank line to echo in the command shell? You might think that putting the ECHO command on a line by itself would do the job, but it doesn’t. Typing echo on a line by itself displays the status of command echoing as mentioned in the previous tip. Typing echo followed by a space doesn’t work either, because the Windows command line treats spaces (in this situation) as meaningless and you get the same results as typing echo followed by nothing at all. To get ECHO to display a blank line, you must enter echo. (Note that there is no space between the period and the ECHO command.)

Fine-Tuning Command Echo with @

The @ command prevents commands from echoing to the output on a line-by- line basis and you can think of it as a line-specific echo off statement. You could use @ to turn off command echoing like this:
@echo The system host name is:
@hostname
Using @, the output that shows the command prompt and commands like this:
C:\>echo The system host name is:
The system host name is:
C:\>hostname
mailer1
becomes
The system host name is:
mailer1
But the real value of @ is that it allows you to tell the command shell not to display the command prompt or ECHO OFF command, and thereby ensures that the only output of your scripts is the output of the commands you enter. Here is an example of a script that uses @ to hide the ECHO OFF command so that it isn’t displayed in the output:
@echo off
echo The system host name is:
hostname
The output from this script is
The system host name is:
mailer1
Tip 
I recommend using @echo off at the beginning of all your command-line scripts. By the way, if you start a command shell and type @echo off, you can turn off the display of the command prompt as well.

Setting the Console Window Title and Colors

If you’re going to take the time to write a script, you might as well add a few special features to jazz it up. Some of the basic techniques that I’ve already discussed are using the ECHO OFF command and clearing the console window before you write output. You may also want to set a title for the window or change the colors the window uses.
The title bar for the command shell is located at the top of the console window. Normally, this title bar displays “Command Prompt” or the file path to the command shell. You can customize the title using the TITLE command. This command works much like the ECHO command in that it displays whatever text follows it on the console’s title bar. For example, if you wanted to set the title of the current console to “System Information,” you could do this by entering the following at the command line:
title System Information
You can not only use the TITLE command to show the name of the script that is running; you can also use TITLE to show the progress of the script as it executes, such as
rem add blocks of work commands
title Gathering Information
rem add blocks of logging commands
title Logging System Information
By default, the console window displays white text on a black background. As you learned in Chapter 1, “Overview of the Windows Command Line,” you can modify this behavior using the Colors tab of the Command Prompt Properties dialog box. You can also set console colors by using the COLOR command. You do this by passing the command a 2-digit hexadecimal code. The first digit corresponds to the background color and the second digit corresponds to the text color, such as
color 21
which sets the text to blue and the background color to green.
The color codes you can use with the COLOR command are shown in Table 3-1. Keep in mind that you can’t set the text and background colors to the same value. If you try to do this, the color doesn’t change. Additionally, you can restore the default colors at any time by using the COLOR command without any arguments, such as
color
Table 3-1: Color Codes for the Command Shell Window
Code
Color
Code
Color
0
Black
8
Gray
1
Blue
9
Bright Blue
2
Green
A
Bright Green
3
Aqua
B
Bright Aqua
4
Red
C
Bright Red
5
Purple
D
Bright Purple
6
Yellow
E
Bright Yellow
7
White
F
Bright White

Managing Command Shell

Managing Command Shell Startup

When you previously worked with the command line, you probably started it by clicking Start, pointing to Programs or All Programs, Accessories, and then choosing Command Prompt. Another way to start a command line is to use the Run dialog box or type cmd in an open command-shell window. These techniques enable you to pass arguments to the command line, including switches that control how the command line works as well as parameters that execute additional commands. For example, you can start the command shell in quiet mode (meaning command echo is turned off) by using the startup command cmd /q and if you wanted the command shell to execute a command and then terminate, you could type cmd /c followed by the command text enclosed in quotation marks. The following example starts a command shell, sends the output of ipconfig to a file, and then exits the command shell:

cmd /c "ipconfig > c:\ipconfig.txt"

Table 2-1 summarizes the key parameters for the Windows command shell (Cmd.exe). Note that several command-line parameters are set by default. Because of this, the command line normally uses standard ANSI character codes for command output, as opposed to Unicode character codes, and enables command extensions that add features to most built-in commands.

Table 2-1: Essential Parameters for the Command Line
Parameter
Description
/C
Executes the command specified and then exits the command shell.
/K
Executes the command specified and then remains in interactive mode.
/A
Command output to files (or pipes) is set to ANSI format (default).
/U
Command output to files (or pipes) is set to Unicode.
/U
Turns on quiet mode, meaning command echo is off. By default, command echo is on.
/T:FG
Sets the foreground and background colors for the console window.
/E:ON
Enables command extensions, which is the default.
/E:OFF
Disables command extensions.


Note

Some parameters cannot be used with other switches. For example, you can’t enable both Unicode and ANSI character codes. If you use both /A and /U, or /E:ON and /E:OFF, the command line applies the last option you passed on the command line.
Sometimes you may want to use different environment settings or parameters for a command line and then go back to your original settings without exiting the console window. To do this, you can use a technique called nesting. With nesting, you start a command line within a command line and the nested command line inherits its environment settings from the current command line. You can then modify the environment as necessary and execute commands and scripts using those settings. When you type exit to end the nested command- line instance, you return to the previous command line and the previous environment settings are restored.


Tip 
As you set out to work with the command shell, keep in mind that some characters have special meanings and that whenever the command shell encounters one of these characters, it attempts to carry out the special procedure associated with that character. Special characters include < > ( ) & | @ ^. If you want to use a special character as a regular character, you must escape the special character for the command shell to look at it literally, without invoking the special procedures with which it is associated. The escape character is the caret (^), which is the character above the 6 key on a standard keyboard, and is placed to immediately precede the special character.

Using Windows Support Tools

Using Windows Support Tools

The Windows Support Tools are a collection of utilities for handling everything from system diagnostics to network monitoring. These tools can be installed on, and used with, all versions of Windows Server 2003 and Windows XP Professional. You can install the support tools by completing the following steps:
  1. Insert the appropriate operating system CD-ROM (Windows Server 2003 or Windows XP Professional) into the CD-ROM drive.
    Caution 
    Because the Support Tools installation modifies the Help And Support Center, you should close any instances of this console that are running before you start the installation process. If you don’t do this, the installation will fail.
  2. When the Autorun screen appears, click Perform Additional Tasks, and then click Browse This CD. This starts Windows Explorer.
  3. In Windows Explorer, double-click Support and then double-click Tools.
    Note 
    Throughout this book, I refer to double-clicking, which is the most common technique used for accessing folders and for running programs. With a double click, the first click selects the item and the second click opens/runs the item. In both Windows Server 2003 and in Windows XP Professional, you can also configure single-click open/ run. Here, moving the mouse over the item selects it and a single click opens/runs the item. You can change the mouse click options with the Folder Options utility in the Control Panel. To do this, select the General tab, and then choose Single-Click To Open An Item or Double-Click To Open An Item, as desired.
  4. Double-click Suptools.msi. This starts the Windows Support Tools Setup Wizard. Click Next.
  5. Read the End User License Agreement and then, if you agree and want to continue, click I Agree and then click Next.
  6. Enter your user information, and then click Next.
  7. In Windows XP Professional, you then see the Select An Installation Type page. Select Complete and then click Next.
  8. Select the destination directory for the support tools. The default location is %ProgramFiles%\Support Tools. If you don’t want to use the default location, type a new directory path or click Browse to search for a location. In Windows Server 2003, the support tools use about 24 megabytes (MB) of disk space. Selecting the Complete option when installing the Windows XP Professional version of the support tools requires about 12 MB of disk space.
  9. Click Install Now.
  10. Click Finish on the Completing The Windows Support Tools Setup Wizard page.
After installation you can access the support tools through the Help And Support Center(This figure shows the Windows Server 2003 window, though the Windows XP Professional window is very similar.) Click Start, click Programs or All Programs as appropriate, click Windows Support Tools, and then select Support Tools Help. As the figure shows, the tools are organized by file name, tool name, and category. Clicking a tool name accesses a help page that displays the online help documentation for the tool and that you can also use to run the tool.


Use support tools to perform such tasks as system diagnostics and network monitoring.
Most of the support tools have extensions that allow you to run them from the command line. You will find the executables for the support tools in the installation directory, which by default is %SystemDrive%\Program Files\Support Tools.
Because the system path is updated to include the support tools installation directory, you do not need to be in this directory to execute the support tools. You can, in fact, run the tools at any command prompt regardless of the current directory. As with other Windows utilities and commands, you can display the syntax for a particular support tool by typing the command name following by a space and /?, such as spcheck /?.

Command Line Essentials

Command Line Essentials

Each new version of Windows has extended and enhanced the command line. The changes have been dramatic and they’ve not only improved the performance capabilities of the command line but its versatility as well. Today you can do things with the Windows command line that you simply could not do in previous versions of Windows. To help you put the available options to use in the fastest, most productive manner, the discussion that follows explores command shell options and configuration, as well as providing tips for using the command history.


Understanding the Windows Command Shell

The most commonly used command line is the Windows command shell. The Windows command shell is a 32-bit environment for working with the command line. You’ll find the executable (Cmd.exe) in the %SystemRoot%\System32 directory. Other command lines are available, such as the MS-DOS command shell (Command.com) discussed in the next section.
Note 
%SystemRoot% refers to the SystemRoot environment variable. The Windows operating system has many environment variables, which are used to refer to user-specific and system-specific values. Often, I’ll refer to environment variables using the syntax %VariableName%.
You can start the command shell by using the RUN command. Click Start, select Run, and then enter cmd in the Open field. Or, you can click Start, point to Programs or All Programs (depending on whether you are using the Classic Start Menu or the system’s default Start Menu), Accessories, and then choose Command Prompt.
The environment for the Windows command shell can be initialized in several ways, including passing startup parameters to Cmd.exe or using a custom startup file, which is placed in the %SystemRoot%\System32 directory. Below Figure shows a command shell window. By default, the command line is 80 characters wide and, in Windows Server 2003, the command shell displays 25 lines of text. The default line-count for Windows XP Professional depends on the screen resolution, but always results in at least 25 lines being displayed. When additional text is to be displayed in the command shell window or you enter commands and the command shell’s window is full, the current text is displayed in the window and prior text is scrolled up. If you want to pause the display temporarily when a command is writing output, press Ctrl+S. Afterward, press Ctrl+S to resume or Ctrl+C to terminate execution.


The 32-bit command shell is the primary command-line window you’ll use.
Note 
Custom startup files are used for MS-DOS programs that require special configurations. Prior to Windows XP, these files were named Autoexec.bat and Config.sys. Beginning with Windows XP and continuing with Windows Server 2003, these files are renamed Autoexec.nt and Config.nt. The files also get a new location, in %SystemRoot%\System32.
In this figure from Windows Server 2003, the display text is

Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.
C:\Documents and Settings\Administrator.MAILER1.001>

Here, the command prompt for the command line shows the current working directory, which by default is %UserProfile%, meaning the user profile directory for the current user. A blinking cursor following the command prompt indicates the command line is in interactive mode. In interactive mode, you can type commands directly after the prompt and press Enter to execute them. For example, type dir and then press Enter to get a listing of the current directory.
The command prompt also has a batch mode, which is used when executing a series of commands. In batch mode, the command prompt reads and executes commands one by one. Typically, batch commands are read from a script file, but batch commands can also be entered at the command prompt, such as when you use the FOR command to process each file in a set of files. (You’ll learn more about batch scripts, loops, and command controls “Command Line Scripting Essentials.”)
Whenever you work with the Windows command line, it is important to keep in mind where the commands you are using come from. Native commands (commands built into the operating system by Microsoft) include:
  • Internal commands that exist internally within the command shell and do not have separate executable files
  • External commands that have their own executable files and are normally found in the %SystemRoot%\System32 directory
Following Tables shows a list of internal commands for the command shell (Cmd.exe). Each internal command is followed by a brief description.


Quick Reference to Internal Commands for the Command Shell (Cmd.exe)
Name
Description
assoc
Displays or modifies the current file extension associations.
break
Sets breaks for debugging.
call
Calls a procedure or another script from within a script.
cd (chdir)
Displays the current directory name or changes the location of the current directory.
cls
Clears the command window and erases the screen buffer.
color
Sets the text and background colors of the command shell window.
copy
Copies files from one location to another or concatenates files.
date
Displays or sets the system date.
del (erase)
Deletes the specified file, files, or directory.
dir
Displays a list of subdirectories and files in the current or specified directory.
echo
Displays text strings to the command line; sets command echoing state (on|off).
endlocal
Ends localization of variables.
exit
Exits the command shell.
for
Runs a specified command for each file in a set of files.
ftype
Displays current file types or modifies file types used in file extension associations.
goto
Directs the command interpreter to a labeled line in a batch script.
if
Performs conditional execution of commands.
md (mkdir)
Creates a subdirectory in the current or specified directory.
move
Moves a file or files from the current or designated source directory to a designated target directory. Can also be used to rename a directory.
path
Displays or sets the command path the operating system uses when searching for executables and scripts.
pause
Suspends processing of a batch file and waits for keyboard input.
popd
Makes the directory saved by PUSHD the current directory.
prompt
Sets the text for the command prompt.
pushd
Saves the current directory location and then optionally changes to the specified directory.
rd (rmdir)
Removes a directory or a directory and its subdirectories.
rem
Sets a remark in batch scripts or Config.sys.
ren (rename)
Renames a file or files.
set
Displays current environment variables or sets temporary variables for the current command shell.
setlocal
Marks the start of variable localization in batch scripts.
shift
Shifts the position of replaceable parameters in batch scripts.
start
Starts a separate window to run a specified program or command.
time
Displays or sets the system time.
title
Sets the title for the command-shell window.
type
Displays the contents of a text file.
verify
Causes the operating system to verify files after writing files to disk.
vol
Displays the disk’s volume label and serial number.

The syntax for using any internal (and most external commands) can be obtained by typing the command name followed by /? at the prompt, such as
copy /?

You’ll find there are many more external commands than internal commands, including ones that are very similar to those built into the command line. Most of these similar commands are extended or enhanced in some way. For example, the external XCOPY command is more versatile than the COPY command because it allows you to copy directory trees as well as files and offers many more parameters. With the external SETX command, you can write environment variable changes directly to the Windows registry, which makes the changes permanent rather than temporary as the SET command does.

Tip 
 : You can also use SETX to obtain current Registry key values and write them to a text file. While this command is included with Windows Server 2003, it is available on Windows XP Professional only when you install the Windows XP Professional version of the Windows Support Tools.
Beyond this, the difference between internal and external commands isn’t that important. Many Windows utilities have command-line extensions that allow parameters to be passed to the utility from the command line, and thus are used like external commands. Later in this chapter, I discuss the two key sources for Windows utilities: the Microsoft Windows Support Tools and the Microsoft Windows Server 2003 Resource Kit. You can also find third-party utilities with command-line extensions.

Understanding the MS-DOS Command Shell

The MS-DOS command shell (Command.com) includes 16-bit commands for the MS-DOS subsystem and other subsystems. You can start the MS-DOS command shell using the RUN command. Click Start, select Run, and then enter command in the Open field. Or, within another command line, type command and then press Enter.
Tip 
If you are using the MS-DOS command shell from within Cmd.exe, the command shell title should change to “Command Prompt – Command” to let you know this. When you finish working with Command.com, you can quit the MS-DOS command shell and return to the Windows command line by typing exit.
The environment for the MS-DOS command shell can be initialized in several ways, including passing startup parameters to Command.com and using a Config.nt startup file, which is placed in the %SystemRoot%\System32 folder. As with Cmd.exe, the MS-DOS command window is 80 characters wide and, depending on operating system and screen resolution, displays at least 25 lines of text by default. When you start an MS-DOS command shell, the standard display text is

Microsoft(R) Windows DOS
(C) Copyright Microsoft Corp 1990-2001.
C:\>

As with the Windows command shell, the MS-DOS command shell has interactive and batch processing modes. It also has native commands built in by Microsoft. These commands are divided into two categories:
  • Internal configuration commands  Commands used to configure the MS-DOS subsystem, which should be placed in startup or program information files, such as Config.nt or Autoexec.nt. Configuration commands include BUFFERS, COUNTRY, DEVICE, DEVICEHIGH, DOS, DOSONLY, DRIVEPARM, ECHOCONFIG, FCBS, FILES, INSTALL, LOADHIGH, LASTDRIVE, NTCMDPROMT, SHELL, STACKS, and SWITCHES.                                                
  • Standard external commands  Commands that you can type at the command prompt, place in scripts, and in some cases use in startup files. Standard external commands include APPEND, DEBUG, EDIT, EDLIN, EXE2BIN, EXPAND, FASTOPEN, FORCEDOS, GRAPHICS, LOADFIX, MEM, NLSFUNC, SETVER, and SHARE. These MS-DOS commands can also be run in Cmd.exe.
When you execute other commands in the MS-DOS shell, these commands are passed through to the 32-bit command shell where they are executed. This, for example, is why you can use the internal command COPY from within the MS-DOS shell. One detail to note about the MS-DOS shell is that the standard external commands are not available on 64-bit versions of Windows Server 2003.

Configuring Command-Line Properties

If you use the command shell frequently, you’ll definitely want to customize its properties. For example, you can add buffers so that text scrolled out of the viewing area is accessible. You can resize the command shell, change its fonts, and more.
To get started, click the command-prompt icon at the top of the command-shell window or right-click the console’s display bar, and then select Properties. As below shows, the Command Prompt Properties dialog box has four tabs:


 Configure the command-line properties for your environment.
  • Options  Allows you to configure cursor size, display options, edit options, and command history. Select QuickEdit Mode if you want to use the mouse to cut and paste text within the command window. Clear Insert Mode to use overwrite as the default editing mode. Use the command history to configure how previously used commands are buffered in memory. (You’ll find more on the command history in the next section of this chapter titled,“aWorking with the Command History.” )
    Tip 
    While working with text-only commands and tools, you may want to use Full Screen display mode to reduce the amount of memory used by the command prompt itself. Afterward, type exit to exit the command prompt and return to the Windows desktop.

  • Font  Allows you to set the font size and face used by the command prompt. Raster font sizes are set according to their pixel width and height. For example, the size 8 x 12 is 8 screen pixels wide and 12 screen pixels high. Other fonts are set by point size, such as 10-point Lucida Console. Interestingly, when you select a point size of n, the font will be n pixels high; therefore a 10-point font is 10 screen pixels high. These fonts can be designated as a bold font type as well, which increases their screen pixel width.                                                                                                                                                         
  • Layout  Allows you to set the screen buffer size, window size, and window position. Size the buffer height so that you can easily scroll back through previous listings and script output. A good setting is in the range of 1,000 to 2,000. Size the window height so that you can view more of the command- shell window at one time. A good setting is 45 lines on 800 x 600 screens with a 12-point font. If you want the command-prompt window to be in a specific screen position, clear Let System Position Window and then specify a position for the upper-left corner of the command window using Left and Top.                
  • Colors  Allows you to set the text and background colors used by the command prompt. Screen Text and Screen Background control the respective color settings for the command-prompt window. Popup Text and Popup Background options control the respective color settings for any popup dialog boxes generated when running commands at the command prompt.
When you are finished updating the command-shell properties, click OK. Windows displays a prompt that asks you to specify how the settings should be applied. Either apply the property changes to the current window only or save the property changes for future windows with the same title, which applies to all command lines you access later. You may also see an option to modify the shortcut that started the current window. In this case, any time you start a command line using the applicable shortcut, it will use these settings.

Working with the Command History

The command history buffer is a feature of the Windows command shell (Cmd.exe) that remembers commands you’ve used in the current command line and allows you to access them without having to retype the command text. The maximum number of commands to buffer is set through the command-line Properties dialog box discussed in the previous section. By default, up to 50 commands are stored.
You can change the history size by completing these steps:
  1. Right-click the command shell’s title bar, select Properties, and then click the Options tab.
  2. Use the Buffer Size field to set the maximum number of commands to store in the history and then click OK.
  3. To save the history setting to use with future command lines, select Save Property Changes For Future Windows or Modify Shortcut That Started This Window as appropriate, and then click OK. Otherwise, just click OK.
You can access commands stored in the history in the following ways:
  • Browsing with the arrow keys  Use the up-arrow and down-arrow keys to move up and down through the list of buffered commands. When you find the command you want to use, press Enter to execute it as previously entered. Or you can modify the displayed command text by adding or changing parameters and then pressing Enter.                                                                                               
  • Browsing the command history pop-up window  Press F7 to display a pop-up window that contains a listing of buffered commands. Next, select a command using the arrow keys. (Alternatively, press F9, then the corresponding number on the keyboard, and finally the Enter key.) Execute the selected command by pressing Enter, or press Esc to close the pop-up window without executing a command.                                                                                                                                                     
  • Searching the command history  Enter the first few letters of the command you want to execute and then press F8. The command shell searches through the history for the first command that begins with the characters you’ve entered. Press Enter to execute it. Or, press F8 again to search the history buffer for the next match in the command history.
As you work with the command history, keep in mind that each instance of Cmd.exe has its own set of command buffers. Thus, buffers are only valid in the related command shell context.