Home / Windows 10

Working at the command prompt

To get to the command prompt, run Cmd.exe. You can do this by double-clicking any shortcut for Cmd.exe, but since you like to type, you might find it easiest to press Windows key+R, and then type cmd. To open a second or subsequent Command Prompt window when one is already open, you can type start in the window that's already running.

Running with elevated privileges

Your activities in a Command Prompt session are subject to the same User Account Control (UAC) restrictions as anything else you do in Windows. If you use Command Prompt to launch a program (for example, Regedit) that requires an administrative token, you will be asked to confirm a UAC prompt before moving on. If you plan to run several such tasks from Command Prompt, you might prefer to run Cmd.exe itself with elevated privileges. To do this, right-click any shortcut for Command Prompt and then click Run As Administrator. Alternatively, type cmd in the search box, and then press Ctrl+Shift+Enter. Windows displays the word Administrator in the title bar of any Command Prompt window running with elevated privileges.

Starting Command Prompt at a particular folder

If you run Cmd.exe from a shortcut or from %SystemRoot%\System32, the session begins with that folder as the current directory. (Directory is the MS-DOS-era term for folder, and you will encounter it frequently in command names, help files, and so on.) If you run Cmd from the Start menu, it begins in your %UserProfile% folder. To run a Command Prompt session at a different folder, hold down the Shift key while you right-click the folder in File Explorer. On the shortcut menu, click Open Command Window Here.

Starting Command Prompt and running a command

The /C and /K command-line arguments allow you to start a Command Prompt session and immediately run a command or program. The difference between the two is that Cmd /C commandstring terminates the Command Prompt session as soon as commandstring has finished, whereas Cmd /K commandstring keeps the Command Prompt session open after commandstring has finished. Note the following:

  • You must include either /C or /K if you want to specify a command string as an argument to Cmd. If you type cmd commandstring, the command processor simply ignores commandstring.
  • While commandstring is executing, you can't interact with the command processor. To run a command or program and keep the Command Prompt window interface, use the Start command. For example, to run Mybatch.bat and continue issuing commands while the batch program is running, type
    cmd /k start mybatch.bat
  • If you include other command-line arguments along with /C or /K, /C or /K must be the last argument before commandstring.

Using AutoRun to execute commands when Command Prompt starts

By default, Command Prompt executes on startup whatever it finds in the following two registry values:

  • The AutoRun value in HKLM\Software\Microsoft\Command Processor
  • The AutoRun value in HKCU\Software\Microsoft\Command Processor

The AutoRun value in HKLM affects all user accounts on the current machine. The AutoRun value in HKCU affects only the current user account. If both values are present, both are executed-HKLM before HKCU. Both AutoRun values are of data type REG_SZ, which means they can contain a single string. To execute a sequence of separate Command Prompt statements, therefore, you must use command symbols or store the sequence as a batch program and then use AutoRun to call the batch program.

You can also use group policy objects to specify startup tasks for Command Prompt.

Editing the command line

When working at a command prompt, you often enter the same command multiple times or enter several similar commands. To assist you with repetitive or corrective tasks, Windows includes a feature that recalls previous commands and allows you to edit them on the current command line. Table below lists these editing keys and what they do.

Key		    Function
Up Arrow	    Recalls the previous command in the history
Down Arrow	    Recalls the next command in the history
Page Up		    Recalls the earliest command in the history
Page Down	    Recalls the most command in the history
Left Arrow	    Move left one character
Right Arrow	    Move right one character
Ctrl+Left Arrow	    Move left one word
Ctrl+Right Arrow    Move right one word
Home		    Move to the beginning of the line
End		    Move to the end of the line
Esc		    Clears the current command
F7		    Displays the command history in a scrollable
		    pop-up box
F8		    Displays commamnds that start with the characters
		    currently on the line
Alt+F7		    Clear the command history

Using command symbols

Old-fashioned programs that take all their input from a command line and then run unaided can be useful in a multitasking environment. You can turn them loose to perform complicated processing in the background while you continue to work with other programs in the foreground.

To work better with other programs, many command-line programs follow a set of conventions that control their interaction:

  • By default, programs take all of their input as lines of text typed at the keyboard. But input in the same format also can be redirected from a file or any device capable of sending lines of text.
  • By default, programs send all of their output to the screen as lines of text. But output in the same format also can be redirected to a file or another line-oriented device, such as a printer.
  • Programs set a number (called a return value) when they terminate to indicate the results of the program.

When programs are written according to these rules, you can use the symbols listed in Table below to control a program's input and output or chain programs together.

Symbol	   Function
<	Redirects input
>	Redirects output
>>	Appends redirected output to existing data
|	Pipes output
&	Separate multiple commands in a command line
&&	Runs the commands after && only if the command
	before && is successful
||	Runs the command after || only if the command before || fails
^	Treat the next symbol as a character
(and)	Groups commands

The redirection symbols

Command Prompt sessions in Windows allow you to override the default source for input (the keyboard) or the default destination for output (the screen).

Redirecting output

To redirect output to a file, type the command followed by a greater than sign (>) and the name of the file.

Using two greater than signs (>>) redirects output and appends it to an existing file.

Redirecting input

To redirect input from a file, type the command followed by a less than sign (<) and the name of the file.

Redirecting input and output

You can redirect both input and output in a command line. For example, to use Batch.lst as input to the Sort command and send its output to a file named Sorted.lst, type the following:

Sort < batch.lst > sorted.lst

Standard output and standard error

Programs can be written to send their output either to the standard output device or to the standard error device. Sometimes programs are written to send different types of output to each device. You can't always tell which is which because, by default, both devices are the screen.

The Type command illustrates the difference. When used with wildcards, the Type command sends the name of each matching file to the standard error device and sends the contents of the file to the standard output device. Because they both go to the screen, you see a nice display with each file name followed by its contents.

However, if you try to redirect output to a file by typing something like this:

type *.bat > std.out

the file names still appear on your screen because standard error is still directed to the screen. Only the file contents are redirected to Std.out. Windows allows you to qualify the redirection symbol by preceding it with a number. Use 1> (or simply >) for standard output and 2> for standard error. For example:

type *.bat 2> err.out

This time the file contents go to the screen and the names are redirected to Err.out.

The pipe symbol

The pipe symbol (|) is used to send, or pipe, the output of one program to a second program as the second program's input. Piping is commonly used with the More utility, which displays multiple screenfuls of output one screenful at a time. For example:

help dir | more

This command line uses the output of Help as the input for More. The More command filters out the first screenful of Help output, sends it to the screen as its own output, and then waits for a keystroke before sending more filtered output.

More: Windows 10 Tutorials