Search notes:

cmd.exe

Starting and quitting cmd.exe

A way to start cmd.exe is to press Win+R and then type the three letters cmd, followed by the ENTER key.
On Windows 10, it can also be started by pressing Win+X followed by C. (However, this can be changed in Settings -> Taskbar: the option Replace Command Prompt with Windows PowerShell needs to be turned off for this.
When cmd.exe is started without /D, it first executes the commands that are listed in the registry under HKEY_CURRENT_USER\Software\Microsoft\Command Processor and HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor (value of Autorun).
Some cmd.exe settings, such as its size or colors, are stored in the registry under HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe

Options

/c or /k command Start a new cmd.exe process to execute the specified command and then exit (/c) or keep (/k) the process.
/s Changes treatment of quotes in cmd after /c or /k.
/q Turn echo off.
/d disable autorun commands (found under HKEY_CURRENT_USER\Software\Microsoft\Command Processor)
/a - /u Output is ANSI - Unicode.
/t:bf Set background (b) and foreground (f) color (for example /t:4f is white on red)
/f:on, /f:off Enable or disable file and directory name completion, aka tab-completion.
/e:on, /e:off Enable or disable command extensions
/v:on, /v:off Enable or disable delayed environment variable expansion

%CmdCmdLine%

The new started cmd.exe session stores how it was started in the variable %CmdCmdLine%.

Quitting cmd.exe

The command interpreter can be terminated with the exit command.

Getting some basic help

Usually, a command can be executed with /? to print a short summary of the purpose and options of a command (for example dir /?).
Some basic help about some built-in commands (such as assoc) and a handful of executables (such as findstr.exe) can be obtained with the help.exe utility.
C:\> help
C:\> help assoc
C:\> help findstr

Executing programs and internal commands

The two main usages of cmd.exe is to start programs (executables) or execute built-in (internal) commands.
The difference between executables and built-in commands is that executables can also be started without cmd.exe while the built-in commands require a running cmd.exe to interpret and run them.
If cmd.exe is run interactively, cmd.exe lets the user enter a line of text. As soon as the user hits enter, cmd.exe tries to execute what the user has entered. The first word of the entered line is the name of the executable or command to be executed, the remaining words are considered parameters to the command.
The command or executable might produce text (which it writes to stdout). If this is the case, cmd.exe reads stdout an prints it back in the console.
For example, echo is a built-in command that simply prints the passed parameters:
C:\> echo Hello world
ping.exe, on the other hand, is an executable. Like built-in commands, executables are identified by their first word. In the following execution of ping, the word localhost is passed as a parameter to ping:
C:\> ping localhost
Pinging pc3.tq84 [::1] with 32 bytes of data:
Reply from ::1: time<1ms
…
Note: the name of the executable is PING.EXE, not ping. So, why is it that cmd.exe still is able to find the «correct» executable?
Answer: it is possible because cmd.exe consults the values of the PATHEXT environment variable. It consists of a list of suffixes, separated by semicolons, that are added to the (supposed) name of the executable if cmd.exe is not able to locate the the executable by the given name at first.
Another suffix that is present in PATHEXT by default is .msc. Thus, the device manager (whose complete name is devmgmt.msc) can be opened like so:
C:\> devmgmt
At this point, it should be noted that cmd.exe is able to locate the directories where with the .exe or .msc through the value of the environment variable PATH. This value consists of a set of directories, separated by semicolons. When cmd.exe tries to located an executable, it tries to search them in these specified directories.
A convenient way to determine what cmd.exe would execute is where.exe (which itself is an executable). For example, if called with the argument services, it (typically) responds with two files:
C:\> where services
C:\Windows\System32\services.exe
C:\Windows\System32\services.msc
This is because PATHEXT contains both suffixes, .exe and .msc, and reports both. However, if executed on the command line, only the first one that is found will actually be executed.
If the first word of the entered text is neither a built-in command nor an executable, cmd.exe responds like so:
C:\> doesNotExist foo bar
'doesNotExist' is not recognized as an internal or external command,
operable program or batch file.

TODO

Changing the appearance of cmd.exe

The foreground and background color of a cmd.exe session can be set with color.
The number of columns and rows of the cmd.exe window can be adjusted with mode.com:
C:\> mode con cols=180 lines=41
The shown text can be cleared with cls.

Configuring cmd.exe

Configuring cmd.exe in the registry.
Some settings are stored in %APPDATA%\Microsoft\Windows\Start Menu\Programs\System Tools\Command Prompt.lnk.

Environment variables

Some environment variables control the behavior of some commands or are otherwise related to cmd.exe are
CD evaluates to the current working directory.
ComSpec stores the full path (directory plus filename, for example C:\Windows\system32\cmd.exe).
COPYCMD for copy
DIRCMD for dir.
PROMPT specifies how the prompt is formatted.
See also variables

History

doskey: a basic history utility.
F7 pops up a window that shows the most recently entered commands.

Windows 10

With Windows 10, its possible to use ctrl-c and ctrl-v.
Text can be searched with ctrl-f. If something was found, the selected region can be extended by pressing the shift key and navigating with the arrow keys.
Note that ctrl-f is also the default completion for file names (if enabled).
The %PROMPT% environment variable has now a $e to insert an escape character, thus the prompt can be colorized with ANSI escape sequences.

Keyboard shortcuts

Selecting text linewise

Text can be selected linewise with shift + up/down.

Mark mode

With the ctrl+m keyboard shortcut, mark mode can be entered.
In mark mode, one can navigate through the text with the arrow keys and select portions of the text with the shift key and then copy/paste it.
In mark mode, unfortunately, ctrl+shift+right does not jump to the end of a word.
When a selection is started by pressing ctrl+shift+alt, the selection is in line wrapping mode.
If the selection is started by pressing ctrl+shift, the selection is in block mode.

TODO

The actual console program behind cmd.exe is conhost.exe.
ANSICON promises ANSI escape sequences for Windows console programs, similarly as ANSI.SYS did for MS-DOS.
Starting the management console
C:\> mmc
Starting the device manager:
C:\> devmgmt.msc
C:\> mmc devmgmt.msc
Opening the User Account Control Settings:
C:\> UserAccountControlSettings
The default value for the registry key HKEY_CLASSES_ROOT\Directory\shell\cmd\command seems to be cmd.exe /s /k pushd "%V".
More msc:
azman.msc Authorization manager
certlm.msc Certificates (local computer)
certmgr.msc Certificates (current user
comexp.msc Component services
compmgmt.msc Computer management
devmgmt.msc Device manager
DevModeRunAsUserConfig.msc Start menu and taskbar
diskmgmt.msc Disk management (partition, drive paths)
eventvwr.msc Event viewer
fsmgmt.msc Shared folders
gpedit.msc Local group policy editor
lusrmgr.msc Local users and groups
perfmon.msc Performance monitor
printmanagement.msc
rsop.msc Resultant set of policy
secpol.msc allows to configure the elevation prompt (Local Security Policy).
services.msc Services
taskschd.msc Task scheduler, compare with schtasks.exe
tpm.msc Trusted platform module (TPM)
WF.msc Windows defender firewall
WmiMgmt.msc Windows management instrumentation (WMI)

See also

Commands
batch-files
variables
WinAPI - cmd.exe
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
Determine if Windows is running in a 32 or 64 bit environment.
The appearance and functionality of cmd.exe is controlled by the console host.
Changing the registry such that cmd.exe can be opened from the explorer with a context menu on a directory.
cmd.exe: line continuation
Parsing the command line
rundll32 allows to execute functions from cmd.exe.
The complete path of cmd.exe (typically C:\Windows\system32\cmd.exe) is stored in the environment variable ComSpec.
Compare with the a_comSpec variable of autohotkey.
Windows 10 S does not have cmd.exe.

Index