Windows Command Prompt Parameters
last modified July 14, 2025
The Windows Command Prompt (cmd.exe) accepts various parameters that control its startup behavior and execution environment. These parameters allow customization of the command session, script execution, and command processing. Understanding them is essential for advanced command-line usage and scripting.
Cmd parameters can specify the initial directory, set environment variables, control command echoing, and determine how commands are processed. They enable automation scenarios where precise control over the command environment is required. Parameters can be combined to achieve specific behaviors.
This tutorial covers the most important cmd.exe parameters with practical examples. We'll explore how to use these parameters in different scenarios from simple command execution to complex batch scripting. The examples will demonstrate real-world usage patterns.
Basic Definitions
Before diving into examples, let's define key terms related to cmd parameters:
- Command Prompt (cmd.exe): The command-line interpreter for Windows. It provides a text-based interface to execute commands and run scripts.
- Parameters: Options passed to cmd.exe that modify its behavior. They typically begin with / and appear after the executable name.
- Batch file: A script containing a series of commands executed by cmd. Batch files have .bat or .cmd extensions and support basic programming.
- Environment variables: Named values that affect running processes. Cmd parameters can modify these before command execution begins.
- Command extensions: Additional features that enhance cmd functionality. These can be enabled/disabled via parameters for compatibility.
Running a Command with /c
The /c parameter executes a specified command and then terminates. This is useful for running single commands from another program or script.
@echo off cmd /c echo Hello from cmd /c echo Back in original batch file
This example shows how /c runs a command then returns control. The echo command executes in a new cmd instance which then closes.
cmd /c echo Hello from cmd /c
Starts a new cmd process that executes the echo command. The /c ensures cmd exits after command completion. The original batch continues execution.
echo Back in original batch file
Demonstrates that control returns to the calling batch file after the /c command completes. This line executes in the original context.
C:\Users\Jano>cmd_c_parameter.bat Hello from cmd /c Back in original batch file
The output shows the command executed by /c followed by the continuation of the original batch script.
Running a Script with /k
The /k parameter executes a command or script but keeps the cmd window open afterward. This is useful for debugging or interactive sessions.
@echo off echo Creating test script... echo @echo off > testscript.bat echo echo This is testscript.bat >> testscript.bat echo pause >> testscript.bat start cmd /k testscript.bat echo Main batch continues after cmd /k
This example creates a simple batch file then runs it with /k. The testscript window remains open after execution.
start cmd /k testscript.bat
Starts a new cmd instance with /k, executing testscript.bat. The /k keeps the window open after the script completes, allowing user interaction. The start command opens it in a separate window. The control is returned to the original batch file immediately.
echo Main batch continues after cmd /k
Shows that the original batch file continues execution immediately after launching the /k command, without waiting for it to complete.
C:\Users\Jano>cmd_k_parameter.bat Creating test script... Main batch continues after cmd /k
A separate window opens showing testscript.bat output. The main batch completes while the testscript window remains active.
Handling Quotes with /s
The /s
parameter changes how cmd.exe processes quotation marks in
command strings. It is useful for resolving issues with nested or complex
quoting.
@echo off echo Without /s: cmd /c echo Hello there! echo With /s: cmd /s /c "echo Hello there!"
This example shows how /s
affects the interpretation of quotes.
Without /s
, the command is processed normally. With /s
,
cmd uses special quote handling rules for the entire command string.
cmd /c echo Hello there!
Runs echo Hello there!
normally and outputs the text without quotes.
cmd /s /c "echo Hello there!"
With /s
, the outer quotes are stripped and the command is processed,
resulting in the same output but using different quote parsing rules.
C:\Users\Jano>quotes.cmd Without /s: Hello there! With /s: Hello there!
In this example, both outputs are identical. The /s
parameter becomes more useful with complex nested quotes or special characters
that need careful parsing.
Setting Environment Variables with /v and /x
The /v and /x parameters control how environment variables are expanded. /v enables delayed expansion while /x enables command extensions (default on).
@echo off echo Testing variable expansion: set var=Initial ( set var=New echo Normal expansion: !var! cmd /v /c echo Delayed expansion: !var! )
This example demonstrates delayed variable expansion with /v. It shows how variable values can be accessed at execution time rather than parse time.
echo Normal expansion: !var!
Shows the behavior without delayed expansion. In blocks, variables expand when the block is parsed rather than when each line executes.
cmd /v /c echo Delayed expansion: !var!
Enables delayed expansion with /v, allowing access to the current value of !var! at execution time rather than when the block was parsed.
C:\Users\Jano>cmd_v_x_parameters.bat Testing variable expansion: Normal expansion: !var! Delayed expansion: New
Output demonstrates different expansion times. Delayed expansion (/v) accesses the current value while normal expansion uses the parse-time value.
Source
This tutorial covered essential cmd.exe parameters with practical examples. Mastering these parameters enables precise control over command execution environments and script behavior.