Cmd command

Updated: 11/12/2023 by Computer Hope
cmd command

COMMAND.COM (CMD or cmd.exe in later versions of Windows) is the command-line interpreter for MS-DOS and is required for Microsoft operating systems to function. Without command.com, a computer running a Microsoft operating system would be unable to boot.

When running Windows NT, 2000, XP, Vista, 7, 8, and 10 there are two versions of the command interpreter, command.com, and cmd.exe. Cmd offers additional environment variables than command.com; however, we recommend if you're attempting to run an MS-DOS utility that you utilize the command.com. To use command.com, click Start, Run, and type command.

Availability

Command, or command.com, is the command interpreter, and with early versions of Windows and MS-DOS, you would not be able to use the computer without this file. This file is available on all versions of Microsoft Windows, and later versions of Windows use it to access the MS-DOS shell.

CMD, or CMD.EXE, is the command line shell introduced in Windows NT and available for all the following versions of Microsoft Windows.

Syntax

Windows 2000, XP, Vista, 7, 8, and 10 "cmd" syntax

Starts a new instance of the Windows 2000 or Windows XP command interpreter.

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF] [[/S] [/C | /K] string]
/C Carries out the command specified by string and then terminates.
/K Carries out the command specified by string but remains.
/S Modifies the treatment of string after /C or /K (see below).
/Q Turns echo off.
/D Disable execution of AutoRun commands from registry (see below).
/A Causes the output of internal commands to a pipe or file to be ANSI.
/U Causes the output of internal commands to a pipe or file to be Unicode.
/T:fg Sets the foreground/background colors (see COLOR /? for more info).
/E:ON Enable command extensions (see below).
/E:OFF Disable command extensions (see below).
/F:ON Enable file and directory name completion characters (see below).
/F:OFF Disable file and directory name completion characters (see below).
/V:ON Enable delayed environment variable expansion using c as the delimiter. For example, /V:ON would allow !var! to expand the variable var at execution time. The var syntax expands variables at input time, which is quite a different thing when inside of a FOR loop.
/V:OFF Disable delayed environment expansion.

Note that multiple commands separated by the command separator '&&' are accepted for the string if it's surrounded by quotes. Also, for compatibility reasons, /X is the same as /E:ON, /Y is the same as /E:OFF and /R is the same as /C. Any other switches are ignored.

If /C or /K is specified, the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (") characters:

1. If all the following conditions are met, then quote characters on the command line are preserved:

  • No /S switch.
  • Exactly two quote characters.
  • No special characters between the two quote characters, where special is one of: &<>()@^| characters.
  • There are one or more whitespace characters between the two quote characters.
  • The string between the two quote characters is the name of an executable file.

2. Otherwise, old behavior is to see if the first character is a quote character. If so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character.

If /D was NOT specified on the command line, then when CMD.EXE starts, it looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if either or both are present, they are executed first.

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

Command Extensions are enabled by default. You may also disable extensions for a particular invocation using the /E:OFF switch. Enable or disable extensions for all invocations of CMD.EXE by setting one or both of the following REG_DWORD values to 0x1 or 0x0 in the registry using REGEDT32.EXE.

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\EnableExtensions

or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions

The user-specific setting takes precedence over the machine setting. The command line switches take precedence over the registry settings.

The command extensions involve changes or additions to the following commands.

To get specific details, type <command> /? to view the specifics. Where <command> is the name of the command you want specifics on.

Delayed environment variable expansion is NOT enabled by default. Enable or disable delayed environment variable expansion for a particular invocation of CMD.EXE with the /V:ON or /V:OFF switch. To enable or disable completion for all invocations of CMD.EXE, set one or both of the following REG_DWORD values to 0x1 or 0x0 in the registry using REGEDT32.EXE:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\DelayedExpansion

or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion

The user-specific setting takes precedence over the machine setting. The command line switches take precedence over the registry settings.

If delayed environment variable expansion is enabled, then the exclamation character can substitute the value of an environment variable at execution time.

File and directory name completion is NOT enabled by default. Enable or disable file name completion for a particular invocation of CMD.EXE with the /F:ON or /F:OFF switch. To enable or disable completion for all invocations of CMD.EXE on a machine or user logon session, set one or both of the following REG_DWORD values in the registry using REGEDT32.EXE:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\CompletionChar
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\PathCompletionChar

or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\CompletionChar
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\PathCompletionChar

with the hex value of a control character to use for a particular function (e.g., 0x4 is Ctrl+D and 0x6 is Ctrl+F). The user-specific settings take precedence over the machine settings. The command line switches take precedence over the registry settings.

If completion is enabled with the /F:ON switch, the two control characters used are Ctrl+D for directory name completion and Ctrl+F for file name completion. To disable a particular completion character in the registry, use the value for space (0x20) as it is not a valid control character.

Completion is invoked when you type either of the two control characters. The completion function appends a wildcard character to the left of the cursor if none is present and builds up a list of paths that match. It then displays the first matching path. If no paths match, it beeps and leaves the display alone. After that, repeated pressing of the same control character will cycle through the list of matching paths. Pressing the Shift key with the control character moves through the list backwards. If you edit the line in any way and press the control character again, the saved list of matching paths is discarded and a new one generated. The same occurs if you switch between file and directory name completion. The difference between the two control characters is the file completion character matches both file and directory names, while the directory completion character only matches directory names. If file completion is used on any of the built-in directory commands (CD, MD or RD) then directory completion is assumed.

The completion code deals correctly with file names that contain spaces or other special characters by placing quotes around the matching path. Also, if you back up, then invoke completion from within a line, the text to the right of the cursor at the point completion was invoked is discarded.

The special characters that require quotes are:
<space>
&()[]{}^=;!'+,`~

Early Windows "command" syntax

Starts a new copy of the Windows command interpreter.

COMMAND [[drive:] path] [device] [/E:nnnnn] [/L:nnnn] [/U:nnn] [/P] [/MSG] [/LOW] [/Y [/C|K] command]
[drive:]path Specifies the directory containing COMMAND.COM.
device Specifies the device to use for command input and output.
/E:nnnnn Sets the initial environment size to nnnnn bytes. (nnnnn should be between 256 and 32,768).
/L:nnnn Specifies internal buffers length (requires /P as well). (nnnn should be between 128 and 1,024).
/U:nnn Specifies the input buffer length (requires /P as well). (nnn should be between 128 and 255).
/P Makes the new command interpreter permanent (can't exit).
/MSG Stores all error messages in memory (requires /P as well).
/LOW Forces COMMAND to keep its resident data in low memory.
/Y Steps through the batch program specified by /C or /K. Only available in MS-DOS 6.x and above.
/C command Executes the specified command and returns.
/K command Executes the specified command and continues running.
/Z Display the errorlevel of every command executed. Only available in MS-DOS 7.x and above.

Examples

cmd

This example opens the cmd command shell. If you were already in a command shell and run the "cmd" or "command" command at the command line, a new Windows command line would be opened. Typing "exit" would exit the new command line and return you to the original command line.