The Windows Vista Command Prompt Environment
To open a Command Prompt window, click Start, All Programs, Accessories, Command Prompt. This will open a window in which you can type commands and review output, as shown in Figure 32.1.
Figure 32.1 The Command Prompt window is the gateway to a world of powerful Windows management tools.
The main difference between a standard Windows application and a command-line program—which in Windows is technically called a console program—is that it doesn't use a graphical display or pull-down menus. Instead, you and the program communicate by typing information into the Command Prompt window. You type commands into the Command Prompt window to tell Windows to do something, and the command programs type information back to you. It's a lot like an Instant Messaging program, where two people type back and forth, except here, the other person is a program.
Command-Line Syntax
The prompt part of Command Prompt window refers to the bit of text shown in Figure 32.1. The string of text that ends with > is called a prompt, and it indicates that Windows is ready for you to enter a command. When you type a command and press Enter, Windows does something, and displays another prompt only when it's ready to accept another command. As mentioned previously, it's a sort of dialog between you and the computer. The computer prints the prompt to tell you that it's your turn, and you press Enter to tell the computer it's its turn.
Each command line starts with the name of the program that you want to run, followed by additional information called arguments. Arguments tell the program what specifically you want to do. For example, when you use the delete file command, you have to tell it what files to delete. A delete command line might look like this:
delete unused_document.doc
The first word on the line tells Windows to use the delete program. The rest of the command line is given to the delete program to interpret. delete interprets the remainder as the name(s) of file(s) to delete. So, as you might expect, this command deletes the file named unused_document.doc.
Most commands that accept filenames on the command line allow you to type the characters * and ? as part of the filename. These are called wildcards, and they match any string of characters, or any one character respectively. Here are some examples:
Command | Will delete... |
delete *.doc | All files in the current folder that end with .doc. |
delete b*.doc | All files in the current folder that start with the letter b and end with .doc. For example, this would delete b.doc and berry.doc but not apple.doc. |
delete photo12?.jpg | Files named photo120.jpg, photo121.jpg, and so on, but not photo12.doc. |
Command-line programs often allow you to type additional arguments called options, flags, or switches that change the behavior of the program. (There's no difference between the three names; they just have different historical origins. The terms can be used interchangeably.) For example, you can add /P to a delete command line, as in
delete /p *.doc
This instructs the program to prompt you before deleting each file, so that you can double-check each one before it goes.
Each program interprets the command-line arguments its own way to meet its own needs. A big part of the learning curve of becoming a command-line guru is learning not only what commands are available but also what arguments are required or available for each one. The expected organization of the arguments and options is called the command line's syntax, and the documentation for each command shows the syntax using a format that looks something like this:
DEL[ETE] [/P] [/F] [/S] [/Q] [/A[[:]attributes]] filename [...]
In almost all cases, the following conventions are used:
- On Windows, items shown in uppercase usually can be entered in either uppercase or lowercase. The documentation for the command will usually tell you if this is not so. (This is true only for Windows programs. On the UNIX operating system, or in the Subsystem for UNIX-based Applications on Windows Vista, upper/lowercase matters, and commands must be typed exactly as shown.)
- Boldface indicates parts of the command line that you have to type literally, exactly as shown.
- Italics indicate placeholders that you must replace with appropriate information. For example, in the delete command, the filename item should be replaced with the name of a file that you want to delete. The attributes part of the delete command aren't self-explanatory, and you have to read the command's documentation to find out what to type.
- Square brackets [ ] indicate parts of the command line that are optional. In the delete command you can see that the /P switch is optional. Notice also that the command name itself can either be typed as del or as delete.
- Ellipses ... indicate that the preceding part of the command can be repeated as necessary. In the delete command, you can type as many filenames as necessary, with a space between each name.
You'll eventually memorize the syntax for the commands that you use the most often, and this is when knowing how to use the command line will start paying off in big time savings.
Quoting Filenames with Spaces
In the preceding example of the delete command, we saw that the command allows you to type several filenames on the same command line, with spaces between the names. For example, the command
delete file1.doc file2.doc
deletes files file1.doc and file2.doc. But what if a filename has a space in it? If I try to delete a file named my big file.doc with the command
delete my big file.doc
Windows will try to delete three files, my, big, and file.doc, respectively, and either it will complain that no such files exist, or worse, if I did have files with those names, it would delete them instead of the one I was after. The solution is to surround filenames that have spaces in them with quotation marks ("). What I actually need to type is
delete "my big file.doc"
The Current Drive and Current Directory
When you view folders in Windows Explorer, you may recall that Windows displays the contents of just one folder at a time. You can generally select a drive letter or folder in the left pane's Folder view, and see the files in the right pane. The folder whose contents are shown in the right pane is the Explorer window's current folder. The command-line environment also has the concept of a current folder.
Hmm, did I just say current folder? Folder is a relatively recent term chosen to help make computers seem more user friendly. The original term is directory. The terms are interchangeable. In the command-line world, though, directory is the one used most often, because it appears in the name of many commands: mkdir for "make directory," cd for "change directory," and so on. I'll use the term directory rather than folder in the remainder of this chapter. So let's try that again: The command-line environment also has the concept of a current directory.
When you open a Command Prompt window (as shown in Figure 32.1), Windows shows the name of the current directory just behind the > character. The prompt c:\users\bknittel> indicates that the current directory is c:\users\bknittel. If I type the command
del myfile.doc
Windows looks for myfile.doc in c:\users\bknittel only. You can work around this in several ways:
- You can manipulate files in other folders by specifying an absolute path for the filename. An absolute path starts with the \ character and specifies the location of the file starting at the topmost (or root) directory. For example, you could delete file somefile.doc from the Public Documents directory by typing its full path on the command line:
del \users\public\documents\somefile.doc
- You can also specify relative paths—that is, by omitting the initial \ in the path name, you can specify the location of a file relative to the current directory. When the current directory is c:\users\bknittel, the paths c:\users\bknittel\documents\myfile.doc and documents\myfile.doc are equivalent. The second path is a relative path because it doesn't start with \, and Windows knows to interpret documents as a subdirectory of c:\users\bknittel.
You can change the current directory to another directory, so that you specify files and paths relative to the new location. The command
cd \users\public
changes the current directory to \users\public, so that filenames and/or relative paths use that as a starting point instead of the original location.
Windows also has the concept of a current drive, also called the default drive. In the examples we just used, the drive letter was c:, and Windows looked for the files and directories only on the c: drive. When you specify a path without typing a drive letter at the beginning, Windows looks on the current drive. You can specify an alternative drive letter in any path, or you can change the current drive by typing a command line consisting of a drive letter and a colon, like this:
d:
which makes drive D: the current drive.
This does seem a bit confusing at first, but you'll quickly get used to navigating around your drives and folders (sorry: I mean drives and directories) from the command line.
Editing Command Lines
Some commands require you to type a long series of names, options, and so on, and it's not reasonable to expect you to be able to type these long commands without making a mistake. Windows has built-in command-line editing features that help you correct mistakes, and they also save you a lot of typing when you repeat the same or similar commands several times. The usual cursor keys work as you would expect, as indicated in Table 32.1.
Table 32.1. Cursor Key Editing in the Command Line
Key | Action |
Backspace, Delete | Deletes the character to the left of or under the cursor, respectively. |
Home, End | Moves the cursor to the beginning or end of the typed line, respectively. |
Ins | Toggles between insert mode, where characters typed in the middle of a line shove the characters to the right of the cursor over, and overwrite mode, where characters typed in the middle of a line replace any existing characters. |
Left, right arrow | Moves the cursor one space to the left or right. |
Ctrl+left or right arrow | Moves the cursor one word to the left or right. |
Up, down arrow | Scrolls through previously typed commands. Use these keys to recall a previously typed command; then modify or correct it and press Enter to reissue the command. |
F3 | Recalls the previously issued command from the cursor point to the end of the command. You can use this helpful key when you only need to retype the first few letters of a command. Type the first few letters of the command and then press F3 to bring back the remainder of the previous command. |
When you make a mistake on a command line, it's almost always easier to recall the previous line with the up arrow key and correct it than to retype it from scratch.
Filename Completion
Many commands involve entering filenames or directory names on the command line. The Windows Vista command shell has a feature called Command Line Completion that can really help you enter these names.
Here's how it works. When you're typing a filename or directory name, you can type just the first few letters of the name and then press the Tab key to have Windows fill in the rest of the name. If more than one name matches the letter or letters you typed, Windows will pick the first matching name. If that is not the correct name, just press Tab again, and Windows will erase the first name and replace it with the next matching name. You can keep pressing Enter until the correct name appears. Windows even takes care of adding quotes to the name, if there are spaces in it.
You don't even actually have to type any letters at all—just press Tab, and Windows will type out the first name in the current directory.
If you are specifying a path that involves folder names and/or filenames, you can type the \ character after Windows fills in the first name, and repeat the process with the next sub-folder or filename. For example, to enter the path \Users\public\Documents, you can probably just type \, u, Tab, \, p, Tab, \, d, Tab Tab (two tabs at the end because the first fills in Desktop).
If you remember to use Command Line Completion, you'll save a lot of time and make many fewer typing mistakes.
Redirection and Pipes
Most command-line commands display information in the command window itself. For example, if you open a Command Prompt window and type the command dir (remember to press Enter afterwards), you'll see something like this:
C:\Users\bknittel>dir Volume in drive C has no label. Volume Serial Number is 5C83-2321 Directory of C:\Users\bknittel 10/13/2006 03:51 PM <DIR> . 10/13/2006 03:51 PM <DIR> .. 10/06/2006 04:51 PM <DIR> Contacts 10/11/2006 04:16 PM <DIR> Desktop 10/06/2006 04:51 PM <DIR> Documents 10/06/2006 04:51 PM <DIR> Downloads 10/06/2006 04:52 PM <DIR> Favorites 10/06/2006 04:51 PM <DIR> Links 10/06/2006 04:51 PM <DIR> Music 10/06/2006 04:51 PM <DIR> Pictures 10/06/2006 04:51 PM <DIR> Saved Games 10/06/2006 04:51 PM <DIR> Searches 10/06/2006 04:51 PM <DIR> Videos 0 File(s) 0 bytes 13 Dir(s) 6,370,185,216 bytes free
The display is nice to look at, but sitting there in the window you can't do much with it. Now, type the command
dir > x.txt
This time, nothing prints out. What has happened is that you instructed Windows to direct the output from the dir command into a file named x.txt. If you type the command notepad x.txt you'll see that the directory printout is indeed in there. You can now use file x.txt as you want: print it, edit it, email it, or whatever.
This is called output redirection, and it works with all command-line programs that generate output. The > character followed by a filename instructs Windows to direct a program's output to the specified file.
If you want to run several programs and want to collect the output of each program in the same file, you might try something like this:
dir *.doc > docs.txt dir *.xls > docs.txt dir *.pdf > docs.txt
However, all you'll end up with is the listing from the third command. If the file that you use after > exists when you issue the command, Windows overwrites the file, erasing its previous contents. Thus, the previous commands left us with the results of the last command only.
To properly collect the output of several programs in one file, you can use one of two techniques. The first approach is to use the append function. Whereas > creates a file from scratch, >> will append, or add on to, an existing file, or, if there is no existing file, it will create one. So, our three-directory listing could be done this way:
dir *.doc > docs.txt dir *.xls >> docs.txt dir *.pdf >> docs.txt
We start with > so that we clear out any previous version of docs.txt; then use >> on the next two commands to tack on successive results.
A second way to redirect the output of several commands to one file is to group the commands inside parentheses. The command
(dir *.doc & dir *.xls & dir *.pdf) >docs.txt
will also have the desired effect. The & character separates the commands, and the parentheses tell Windows to redirect the output of all three together.
One other redirection operator that you should know about is called the pipe or pipeline. The | character indicates that the output of one command should be fed as the input of another. For example:
net view | findstr /i core
runs two commands. The first is net view, which lists all of the Windows computers that Windows can see on your network. The | character tells Windows to feed that listing into the second command, findstr /i core. This second command searches whatever text is fed to it and passes through only those text lines that contain the word core in either upper- or lowercase; the /i switch means to ignore case. The net result is that this command line will display the names of any computers on your network that have "core" as part of the computer name.
Although these are the most important special characters that you will use to redirect the output of command-line programs, there are a few others. Table 32.2 lists the characters that have a special meaning when used in a command line.
Table 32.2. Windows Vista Command-Line Special Characters
Character | Commands |
& | Separates multiple commands on the command line. For example the command dir >x.txt & notepad x.txt puts a directory listing into a file and then displays the file with Notepad. |
&& | Separates multiple commands. The command following && runs only if the command preceding && succeeds. |
| | Pipes output of one command to another. The output of the command preceding | is sent as the input to command after |. For example, dir /b /s *.doc | sort | more lists all .doc files in the current directory and all subdirectories; this listing is sent through sort and put into alphabetical order, and the sorted listing is fed to more, which displays it one screen at a time. |
|| | Separates multiple commands. The command following || runs only if the command preceding the || symbol fails. This is the opposite of the && function and has nothing to do with the pipe (|) function. |
() | Groups commands. Used primarily with the if and else commands but can also be used as follows: The command (dir & netstat & ipconfig) | more prints a directory listing and runs two network information tools, and sends the collected output of the three programs through the more screen-at-a-time tool. |
; or , | Separates arguments on a command line. This usage is a carryover from early DOS days, is generally not required, and should not be used unless required by a specific command. |
^ | Escape character, removes the special meaning from whatever character follows it. For example, ^| is treated as ordinary text character |. |
Cut and Paste in the Command Prompt Window
Although you will usually use output redirection to store the results of programs in files, you can also use cut and paste to move text into or out of a Command Prompt window.
To paste text into the window at the cursor location, click the window's System Menu (the upper-left corner) and select Edit, Paste, or type Alt+Space E P.
To copy text from the window to the Clipboard, click the window's System Menu, and select Edit, Mark. Alternatively, type Alt+Space E M. Use the mouse to highlight a rectangular area of the screen; then press Enter. This copies the text to the Clipboard.
By default, the mouse does not select text until you use the Mark sequence. This makes it easier to use MS-DOS programs that are mouse-aware. If you seldom use the mouse with MS-DOS applications, click the System Menu (or press Alt+Space), select Defaults, and check Quick Edit. When Quick Edit is enabled, you can use the mouse to mark text for copying to the Clipboard without having to type Alt+Space E M first.
Although I just had you change the default setting, you can change the Quick Edit setting for just a given open copy of the Command Prompt window by opening its System Menu and clicking Properties rather than Defaults.
Command-Line Tips
The following sections offer some additional tips to help make using the command line easier.
Easy Access to the Command Prompt window
If you're a command-line junkie like I am, drag the Command Prompt start menu item to the Quick Launch bar for easy access.
Running Commands with Elevated Privileges
Some command-line programs require elevated privileges to do their job correctly. To run a command-line program with elevated privileges, you have to run it from a Command Prompt window that is itself running elevated.
To run an elevated Command Prompt window, click Start, All Programs, Accessories. Then right-click Command Prompt and select Run As Administrator.
If you find yourself doing this a lot, you might want to create a shortcut for it. Just drag the Command Prompt menu item to the desktop, or to another location in the Accessories Start menu, being sure to hold down the Ctrl key so that you make a copy. Rename the new shortcut something like "Administrator Command Prompt" or "Elevated Command Prompt." Right-click the new shortcut and select Properties. Click the Advanced button, check Run As Administrator, and then click OK.
Now, you can use this shortcut to open a privileged Command Prompt window. You will have to confirm the User Account Control (UAC) prompt every time you open it.
Change the Default Drive and Directory at the Same Time
You can change the default drive and directory at the same time by adding /d to a cd command. For instance, the command
cd /d e:\setup
changes the current drive to e: and the current directory to \setup at the same time.
Use pushd and popd to Make Backtracking Easier
As you use the cd command to burrow through your computer's drive and directories, you'll quickly start to miss the Explorer's Back button when you find yourself having to type long path names to get from one directory to another and back again.
The commands pushd and popd were written to help make this easier. The command pushd works just like cd, except that Windows remembers the previous current directory. popd returns you to the current directory before the last pushd. Here's how it can work, assuming that the initial directory is c:\users\bknittel:
Command | New Current Drive and Directory |
pushd \users\public\documents | c:\users\public\documents |
popd | c:\users\bknittel |
pushd \windows\system32 | c:\windows\system32 |
pushd e:\setup | e:\setup |
popd | c:\windows\system32 |
popd | c:\users\bknittel |
Note that pushd lets you change the current drive letter without having to type /d.
Use pushd to Use Network Folders
If you're working with a network, pushd also lets you specify a path to shared folder. The command
pushd \\myserver\public
automatically maps a drive letter to the shared folder named public on computer myserver and makes this the current drive and directory. pushd starts by mapping drive Z: and works its way backward toward A from there. popd unmaps the drive.
Getting Information About Command-Line Programs
If you haven't used the command line since Windows 98 or Me or even MS-DOS, you'll find that built-in commands have been significantly enhanced. To see a list of command-line utilities along with syntax and usage examples, open the Help and Support Center and search for Command-Line Reference. If you see an entry titled Command Line Reference A-Z, that's the one you want. If that's not present, select Command Line Reference for IT Pros, which will lead you to Microsoft's website and the A-Z reference for Windows XP and Windows Server 2003 but this will hopefully be updated to include Vista.
To get information on a specific command that interests you, try the following sources, in the order listed. I'll use the rasdial command in the examples that follow, but you can use the same technique with any command that interests you.
A majority of command-line commands will print help information if you add /? to the command line. For example, to get information for the rasdial command, type rasdial /?.
If the command prints so much text that it scrolls out of view, use one of the following techniques to read it all:
- Use the Command Prompt window's scrollbars to back up.
- Press F3 to recall the command line, add | more to the end of the line, and press Enter. This will run the help listing through the more command, which displays it one screen full at a time. Press Enter after reading each screen.
- Direct the help information to a file and then view the file in Notepad by typing two commands, for example:
rasdial /? >x notepad x
- Type the command help rasdial. If too much text prints, use the techniques just listed to manage the overflow.
- Click Start, Windows Help and Support, and search for rasdial.
- Open Internet Explorer and type rasdial in the Search window.
- Browse to www.google.com, and try the following searches, in this order:
rasdial site:microsoft.com windows rasdial rasdial.exe rasdial
Not every one of those information sources will work for every command, but at least one should lead you to an explanation of what the command does, what its command-line options are, and some examples of its use.