cmd - Start the command processor
Table of Contents
What is the cmd command?
The cmd command starts cmd.exe, the Windows command processor. Typing cmd at a Command Prompt starts a new Command Prompt (a child process).
With options such as cmd /c "command", you can also run a single command and exit. This is essential when you need to call internal commands (dir, copy, mklink, and so on) from outside cmd.exe, such as from PowerShell, Task Scheduler, forfiles, or a shortcut.
This page explains the basic usage of the cmd command, its main options, and the quoting rules, with sample code.
How to use the cmd command
cmd [/c | /k] [/s] [/q] [/d] [/a | /u] [/e:on | /e:off] [/f:on | /f:off] [/v:on | /v:off] [<string>]
| Option | Description |
|---|---|
/c <string> | Runs the string as a command, then exits. |
/k <string> | Runs the string as a command, then stays open at the prompt. |
/s | Changes how quotation marks in the string after /c or /k are handled (see below). |
/q | Turns echo off (same effect as @echo off). |
/t:<BF> | Sets the background and foreground colors as two hex digits (same format as the color command). |
/d | Disables execution of the AutoRun commands in the registry. |
/a | Outputs the results of internal commands to a pipe or file in ANSI. |
/u | Outputs the results of internal commands to a pipe or file in Unicode (UTF-16). |
/e:on, /e:off | Enables or disables command extensions. |
/f:on, /f:off | Enables or disables file and folder name completion. |
/v:on, /v:off | Enables or disables delayed environment variable expansion. |
Because /c and /k treat the rest of the line as the command, they must come last, after the other options.
You can also see the list of options with cmd /?.
Start a new Command Prompt
Running cmd without options starts another cmd.exe inside the current Command Prompt.
Running exit here ends the child cmd.exe and returns you to the original Command Prompt.
Environment variables you set inside the child cmd.exe are not reflected in the parent after you return with exit. Environment variables set in the parent are inherited by the child.
For more on exiting, see the following page.
Run a command and exit with /c
With /c, cmd.exe runs the specified command and then exits.
cmd /c echo Hello
To run several commands, enclose the whole thing in quotation marks.
cmd /c "cd /d C:\Windows && dir /b *.exe"
If you pass &&, |, >, and similar characters to cmd /c without quoting them, the calling shell (cmd.exe or PowerShell) interprets them first. See the following page for details.
Run internal commands from PowerShell
Commands implemented as internal commands of cmd.exe, such as mklink and assoc, cannot be run directly from PowerShell. You can run them with cmd /c.
cmd /c mklink /d C:\work\assets D:\shared\assets
Pass along the exit code
The exit code of the command run with cmd /c becomes the exit code of cmd.exe. The caller can therefore check the result with %errorlevel% (or $LASTEXITCODE in PowerShell).
When you give the exit command an exit code, that value becomes the exit code of cmd.exe. Exit codes are explained on the following page.
If you accidentally overwrote %errorlevel% with something like set errorlevel=5, delete the variable with set errorlevel= and the real exit code is referenced again.
Run a command and keep the window open with /k
With /k, cmd.exe does not exit after running the command and waits for further input.
cmd /k "cd /d C:\work && dir"
If you set a shortcut’s target to cmd /k "C:\scripts\check.cmd", you can check the results after the batch file finishes. You can also use it from the Run dialog (Win+R).
To open it in a new window, combine it with the start command.
start "Log monitor" cmd /k "title Log monitor && echo Monitoring started"
Enable delayed expansion with /v:on
Starting with /v:on enables delayed expansion (!VariableName!), which expands environment variables at run time. This gives you the same effect as setlocal enabledelayedexpansion in a batch file, but on the command line.
cmd /v:on /c "set "MSG=Hello" && echo !MSG!"
Without /v:on, setting a variable and referencing it as %MSG% on the same line does not give the intended value, because %MSG% is expanded when the line is parsed. See the following page for details on delayed expansion.
Other options
/q Turn echo off
With /q, command echoing is disabled, just as if you had specified @echo off.
cmd /q /c "echo test"
/d Disable AutoRun
If a command is registered in the AutoRun value under HKCU\Software\Microsoft\Command Processor (or HKLM) in the registry, it runs whenever cmd.exe starts. With /d, you can start without running it. This is useful for troubleshooting when startup is slow or you suspect AutoRun is causing odd behavior.
/u Output in Unicode
With /u, the output of internal commands sent through a pipe or redirection is Unicode (UTF-16). If you save it to a file by redirection, you get a UTF-16 file. For details on character encoding, see the following page.
Watch out for the quoting rules
When the string after /c or /k starts with a quotation mark ("), cmd.exe handles the quotation marks according to these rules:
- If
/sis not specified and all of the following conditions are met, the quotation marks are kept as they are.- There are exactly two quotation marks.
- There are no special characters (
&<>()@^|) between them. - There is at least one whitespace character between them.
- The string between them is the name of an executable file.
- Otherwise, the first quotation mark and the last quotation mark are removed, and the string between them is run.
So when you run a path that contains spaces, it is safest to wrap the whole thing in another pair of quotation marks.
cmd /c ""C:\Program Files\My App\run.cmd" "argument 1""
With /s, cmd.exe always applies rule 2 (remove the first and last quotation marks) without checking the conditions, so the handling is easier to predict.
cmd /s /c ""C:\Program Files\My App\run.cmd" "argument 1""
Simple cases such as cmd /c "dir" are not a problem, but when the string contains several quotation marks or &, quotation marks may be removed in unintended places. If it does not work, wrap the whole thing in one more pair of quotation marks and add /s to check.