How-to: CMD Shell

Notes on working with the Windows CMD shell.

Pausing or stopping a batch script

Execution of any batch script can be paused by pressing CTRL-S this also works for pausing a single command such as a DIR listing. Pressing any key will resume the operation.

Execution of any batch script can be canceled by pressing CTRL-C

If one batch file CALLs another batch file CTRL-C will exit both batch scripts.
If CMD /c is used to call one batch file from another then CTRL-C will cause only one of the batch scripts to terminate. (see also EXIT)

Copy and Paste / QuickEdit

In Windows 10 the standard Ctrl-X & Ctrl-C shortcuts are supported.

For earlier versions of Windows you can enable QuickEdit cut and paste at the Command Prompt as follows:

Activate the control menu at the top left of the current cmd window, go to Properties, Options tab and then tick against QuickEdit Mode. Now you can select text with the mouse and hit Enter (or right click) to copy it to the clipboard. Paste anywhere using Control+V (or Right Click) or via the menu.

ESC will cancel any selection and return to editing mode.
When copying between windows, you need one click to select the window and a second click to paste.

Command Line Auto Completion

Pressing TAB once at the command line will display the first matching file/folder, pressing TAB again and you will cycle through the other matching files and sub-directories. TAB can be used half way through typing a command, wherever you need the filename to appear.

If you type the beginning of a file/folder name, then the TAB completion will be limited to only show matching names:

DIR c:\pro <TAB>

At the command prompt Ctrl-D gives folder name completion and Ctrl-F gives File and folder name completion.

Adding SHIFT to Ctrl will move through the list backwards.

History of Previous Commands

Pressing the UP arrow will list previous commands entered at the command prompt.
Other DOSKEY function keys are loaded by default (F7, F8, F9)

"Quote" Characters in a command

If /C or /K is specified, then the remainder of the command line is interpreted as a command and the following logic is used to process quote (") characters:

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

- No /S switch (Strip quotes)
- Exactly two quote characters
- No special characters between the two quote characters, where special is one of: & < >( ) @ ^ |
- There are one or more whitespace characters between the 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 and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character. To negate this behaviour use a double set of quotes "" at the start and end of the command line.

CMD /c ""c:\work\my reports\profit ^& Loss.doc"" vs CMD.exe

All the commands on these pages assume you are running the 32 bit or 64 bit command line (cmd.exe)

The old 16 bit command processor is supplied to provide backward compatibility for 16 bit DOS applications. has very limited functionality compared to cmd.exe e.g. it will fail to set an %errorlevel% after many commands.

If you name your batch scripts with the extension .CMD rather than .BAT then they will not run under even if copied to a Windows 95 machine.

The %COMSPEC% environment variable will show if you are running CMD.EXE or

One key difference between .CMD and .BAT scripts (running under CMD.EXE) is that with extensions enabled, commands like PATH/APPEND/PROMPT/SET/ASSOC will reset ERRORLEVEL to 0 if they succeed. In the old style .BAT file, the ERRORLEVEL will not be changed unless there is a new error (source).

On 64 bit versions of Windows, the 32 bit CMD.exe can be found at %windir%\SysWoW64\cmd.exe To reduce compatibility issues, the WOW64 subsystem isolates 32-bit binaries from 64-bit binaries by redirecting registry calls and some file system calls.

Registry Keys:

Allow UNC paths at command prompt
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]

Run a command when CMD.exe starts
[HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor]

Activate Automatic Completion (this is on by default for all recent versions of Windows)
[HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor]

For Windows 7: Add an elevated 'Open CMD prompt here (Admin)' option to the 
context menu for file system folders:
@="Open CMD prompt here (Admin)" [HKEY_CLASSES_ROOT\Directory\shell\runas\command] @="cmd.exe /k pushd %L" For Windows 7: Add an elevated 'Open CMD prompt here (Admin)' option to the My Computer context menu: [HKEY_CLASSES_ROOT\CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}\shell\runas] @="Open CMD prompt here (Admin)" [HKEY_CLASSES_ROOT\CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}\shell\runas\command] @="cmd.exe"

For Windows 10
See this article on TenForums

Running multiple instances of CMD.exe

At the command line or in a batch script CMD will start a new instance of CMD.exe which will appear in the same window. The EXIT command will close the second CMD instance and return to the previous shell.

A method of calling one Batch script from another is to run a command like CMD /c C:\docs\myscript.cmd

The output of CMD can be redirected into a text file. When CMD /c is used, the EXIT command is not required.

The native version of CMD.exe is always in %windir%\system32, on 64 bit Operating Systems there is also a 32 bit CMD.exe in %windir%\SysWOW64

Long Commands and long filenames

The maximum length of any command line (or variable) within CMD is 8191 characters.
For all versions of Windows, NTFS and FAT allows pathnames of up to 260 characters.

A workaround for the limited pathname length is to prefix \\?\
for example:

The environment Variable %CmdCmdLine% will expand into the original command line calling CMD.EXE or a batch script.

“The method of the enterprising is to plan with audacity and execute with vigor” ~ John Christian Bovee

Related commands

CMD - Start a new CMD shell.
How-to: Run a script - How to create and run a batch file.
How-to: CMD Internal - Commands that are Internal to the CMD shell.
Clink - Powerful Bash-style command line editing/auto-complete for cmd.exe
ConEmu-Maximus5 - a Windows console emulator with tabs.

Copyright © 1999-2024
Some rights reserved