cmd - Start the command processor

Maintained on
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>]
OptionDescription
/c <string>Runs the string as a command, then exits.
/k <string>Runs the string as a command, then stays open at the prompt.
/sChanges how quotation marks in the string after /c or /k are handled (see below).
/qTurns 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).
/dDisables execution of the AutoRun commands in the registry.
/aOutputs the results of internal commands to a pipe or file in ANSI.
/uOutputs the results of internal commands to a pipe or file in Unicode (UTF-16).
/e:on, /e:offEnables or disables command extensions.
/f:on, /f:offEnables or disables file and folder name completion.
/v:on, /v:offEnables 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 /?.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>cmd /?

Start a new Command Prompt

Running cmd without options starts another cmd.exe inside the current Command Prompt.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>cmd
Microsoft Windows [Version 10.0.26100.1]
(c) Microsoft Corporation. All rights reserved.

Running exit here ends the child cmd.exe and returns you to the original Command Prompt.

Environment variable changes in a child process

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
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>cmd /c echo Hello
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).

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>cmd /c exit 3
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %errorlevel%
3

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.

Tip

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!"
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>cmd /v:on /c "set "MSG=Hello" && echo !MSG!"
Hello

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:

  1. If /s is 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.
  2. 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""
Understand how quotation marks are removed

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.

Quiz

Practice Problem

Which option runs one command and then leaves the Command Prompt open and waiting for input?

Choose one answer.

  1. /c
  2. /k
  3. /q

Practice Problem

How can you run an internal cmd.exe command such as mklink from PowerShell?

Choose one answer.

  1. Type mklink as is
  2. Run it like cmd /c mklink …
  3. Run it like cmd /q mklink …

Practice Problem

What happens to an environment variable you set in a child Command Prompt started with cmd after you return to the parent with exit?

Choose one answer.

  1. It is also reflected in the parent
  2. It is not reflected in the parent
  3. It is saved to the registry
#Command Prompt #Batch Files #Options #Command Line #Commands