Tips for Using the Windows Command Prompt
Here's an assortment of tips and tricks for using the Windows command shell.
There are certain little tricks that books on the command prompt don't always tell you. Or, if they do, the description is buried away in a paragraph somewhere. Experienced users of the command line know all about these. However, average users may not and I am going to mention a few useful tips for them. As far as I know the tips work in Windows XP, Vista, and Windows 7 except where noted. Tips for Vista/7 only are given on another page.

How to make a blank line in a batch file

Sometimes you would like a blank line or two in the output from a batch file. It isn't immediately clear how to do this. Simply entering "echo" doesn't work because that will output the status of command echoing. The trick is to enter echo. Note that "echo" is followed by a period with no space in between.

Force the "echo" command not to parse arguments

The preceding tip is a special case of a more general method for using the "echo" command. Although the command is used to display text or messages, it can also take certain arguments such as "on" or "off". If you write
echo off
you will not get a display of the string "off" but will actually be configuring the "echo" command itself. To display the string "off", you would use echo. off In other words, placing a period at the end of "echo" forces the command to simply display whatever follows without checking to see if the string is one of the special cases.

Check if a file exists

A special variant of the "If" statement can be used to find out if a file is already present. The statement is if exist somefile somecommand The statement can also test for non-existence of a file with if not exist somefile somecommand

The useful device "nul"

The invisible null device called "nul" has a number of uses. (It's also sometimes called the "bit bucket" or the "black hole".) Anything sent to it disappears. It can be used in statements when you do not want output to be dIsplayed. For example the command somecommand > nul will carry out some command but send whatever is the normal output into oblivion. Sometimes in a batch file you do not want any possible error messages to be displayed. This is done by using somecommand 2> nul

Stopping a runaway command

Sometimes you start a command only to find that it is going on and on, spewing out screen after screen of output. Most of the time you can terminate a command by simultaneously pressing the two keys "Ctrl" and "c".

Pausing a scrolling screen

If you have a command with a lot of output,, you can pause the scrolling so that you can read what's on the screen. Use the keyboard combination "Ctrl+s". To resume scrolling, repeat Ctrl+s

Use drag and drop

Having to type the fully qualified path of a file every time it's needed in a command can be tedious and subject to error. Many people are unaware that a file can be dragged from a folder or Windows Explorer view and dropped on an open command window. It saves a lot of typing. (Doesn't work in Vista)

Go up one level above the working directory

Any Unix user knows this one but it's often new to Windows users. To go up to the directory that is one level above the working directory, enter cd .. You can repeat this to go up more levels. It's a little off the subject of the command shell but in the Start-Run line just entering the two periods ".." will also take you up one level from the default working directory (the working directory is normally
%USERPROFILE%)

How to get to the C: drive

If you want to use the C: drive as the working directory, enter cd .. twice. Note that either cd C: or pushd C: do not work.

General way to get to the root folder

In general, you can get to the root or drive by the command cd \ (Thanks to Michael Leonard for suggesting this tip.)

How to change the working directory to a folder on a different drive

If you want to change the working directory for a command window to a folder on a different drive, the command "cd" doesn't work. You have to first enter the drive letter and colon and then enter "cd" and the folder path. However, you can use the switch /d to change the current working directory drive as shown below: cd /d E:\testYou can also make the change with one command entry if you use "pushd" instead of "cd": pushd E:\test

Watch out for spaces in file and folder names

The command shell does not recognize spaces in path names. Any path name with spaces must be enclosed in quotation marks. This problem often crops up in scripts where certain environment variables or input arguments are used. For safety, variables that involve file or folder names should be enclosed in quotes.

Special treatment of variables in "For" statements in batch files

"For" statements are very useful, providing powerful iterative methods. They have the peculiarity, however, of requiring double percent signs for iteration variables in batch files. in other words the syntax in a batch file is:for %%variable In set Do statementIf a "For" loop is run directly from the command line, only a single percent sign is used. The syntax is then: for %variable In set Do statement

Case-sensitive variables in "For" statements

In contrast to Unix systems, Windows is usually not case-sensitive, However, iteration variables in "For" statements are case-dependent. So %A is a different variable from %a.

Pin a command-line shortcut to the Start menu

If you use the command prompt frequently, make it easily accessible. Open Start-All Programs-Accessories and right-click the entry "Command Prompt". Select "Pin to Start menu" from the context menu. Or go to \WINDOWS\system32 and right-click the command shell file cmd.exe and select "Pin to Start menu" from the context menu.

Create a shortcut to a command

If there is a command that you use frequently, you can create a shortcut. The trick is to use the switch /k so that the command prompt stays open. The entry for the shortcut should be cmd /k somecommand.exeIf the command also needs switches, those can added as well. (The general details of making a shortcut are at this page.)

Open Windows Explorer from the command line

To open the current command-line directory in a Windows Explorer window use the command start .To open the directory above the current command-line directory in a Windows Explorer window use the commandstart ..(Windows XP only) To open My Computer in a Windows Explorer window use the commandstart ...

Using the command "Start"

The tip given above is an example of how the "Start" command can be used to invoke an action or a system folder or an URL. For example, simply entering "cookies" in the Run line will open the system folder Internet Cookies in Windows XP (but not in Vista). However, in the command shell, you would need to enter start cookies In Vista/7, the command has to be modified with the Shell command and would be start shell:cookies Similarly, you can open a program like Microsoft Word with the commandstart winwordYou can also open a Web page in Internet Explorer with a command of the typestart http://somesite.com

Save typing with file-name and folder-name completion (Tab completion)

A very useful feature that can save a lot of typing is the name or path completion function. This feature uses the Tab key to complete file and folder names that you begin typing. For example, type "a" (no quotes) into a command line and then press the Tab key. Windows will complete your typing with the name of an existing file or folder beginning with "a", starting in alphabetic order. Press Tab again and the next possible file or folder will be displayed. In this way, you can cycle through all files and folders existing in your current path that begin with a particular character or group of characters. The keyboard pair Shift + Tab will take you backwards in the list. The tab completion function can be used in more than one place in a command.

Enable QuickEdit mode for the command window

Being able to cut and paste to and from the command window is very handy but it is not enabled by default. I use this feature frequently and I suggest that you enable it for all command windows. The details of how to enable QuickEdit are given on another page. Once QuickEdit is enabled, the contents of the clipboard can be entered into a command prompt by right-clicking in the command window.

Display the Command History

The default setting for the configuration of a command window includes the capability for storing up to 50 previously entered commands. The command history can be displayed by entering the "F7" key.

Use the "sleep" command in Windows XP batch files

Sometimes it is desirable to have a batch file wait a certain amount of time before it carries out the next command. If you download the free Windows 2003 Server tools (described on another page), one of the available tools is sleep.exe, which provides a way to make batch files wait a specified interval. For an interval of n seconds the command is: sleep n

Copy text from the console window

Way back in the days of DOS, it was not uncommon to enter text directly from the command window into a file with the "copy" command. That is less common in Windows but the capability is still there. Output from the command window or console is denoted by CON. (It is not case-sensitve.) To copy text from the command window to a file "sometext.txt", the sequence of statements would be copy con sometext.txt
First line of your desired text
some more text...
^Z
The last line indicates the keyboard combination of the Control key and "z" followed by pressing the Enter key. This command terminates the sequence and sends the text to the desired file, which it creates. This particular example places the file in the working directory but other paths can be used.

Use Driverquery to list the drivers on your system in a spreadsheet

To create a list of installed drivers in a form that can be read as a spreadsheet, use the command (with administrator privileges)driverquery /v /fo csv > "%userprofile%\drvlist.csv" To list signed drivers use driverquery /fo csv /si >"%userprofile%\drvlist.csv"

In both cases, the file is placed in the User folder. Modify the location and /or the name of the created file to suit your individual preferences.

(Not available in Windows XP Home)

Tips for the Vista/Windows 7 command shell

Windows XP and Vista/7 share many of the same features in the command line. However, as to be expected, there are some differences. Tips that are relevant to Vista/7 only are given on the next page.

Back to top