Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For modern Windows batch scripting, use cmd.exe and .bat or .cmd files. The “NT” label reflects Windows’ historical lineage; Microsoft’s current cmd reference applies to Windows 10 and 11, Windows Server 2016, 2019, 2022, and 2025, and Azure Local 2311.2 and later. The key to reliable scripts is controlling how the shell starts, quoting paths, understanding when variables expand, and checking a command’s exit status before it changes.

What is the difference between a command session and a script?

A command session is an interactive cmd.exe process: you enter commands at its prompt and see their output there. A batch script is a text file containing commands that cmd.exe processes in sequence. You can type a command at the prompt, run a script from that prompt, or start a new command-interpreter process to run a command or script.

That distinction matters because running a script inside an existing session can affect its environment, while starting a new process gives the commands a separate shell process. Use setlocal in a script when you want environment changes to be confined to the script’s execution.

How do you run a batch file with cmd?

Use cmd switches to determine whether the shell runs a command and exits or stays open, and to control startup behavior. Microsoft documents these switches in its cmd reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Invocation Behavior Useful when
cmd /c "C:Scriptsdaily job.cmd" Runs the command, then exits the new command interpreter. You want a one-shot command or script run.
cmd /k "C:Scriptsdaily job.cmd" Runs the command, then keeps the new command interpreter open. You want to inspect output or continue working at the prompt.
cmd /d /c "C:Scriptsdaily job.cmd" Runs the command and exits, with AutoRun commands disabled for that process. You want to avoid machine- or user-configured AutoRun commands affecting the launch.

With no /d, cmd checks machine and user AutoRun registry entries before other variables. Use /d for a controlled launch when those entries should not run; do not edit registry entries casually. Microsoft warns that incorrect registry changes can severely damage a system.

/e controls command extensions and /v controls delayed expansion for the command-interpreter process. If a script relies on a particular setting, specify it deliberately rather than assuming the caller’s shell has the desired configuration. For example, cmd /d /e:on /v:off /c "C:Scriptsdaily job.cmd" starts with AutoRun disabled, extensions on, and delayed expansion off.

How should you quote paths and special characters?

Put a path containing spaces in double quotes when passing it as a command or argument, such as "C:Program FilesExample Apprun.exe". In a batch file, quote the executable path too: "C:Program FilesExample Apprun.exe" "C:Datainput file.txt".

Characters such as &, |, parentheses, and redirection characters have meaning to the shell. They may separate commands, form a pipeline, group commands, or redirect output instead of being passed literally. Quote or escape such characters as appropriate for the specific command and parsing context; a caret (^) can escape metacharacters in some contexts, but it is not a universal solution.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Nested uses of cmd /c, batch files, and program arguments can add parsing layers. In particular, cmd has special quote-handling rules with /s. Check the intended command’s parsing behavior rather than assuming that enclosing the whole command in quotes will preserve every embedded argument.

How do command operators control what runs next?

Use conditional operators when a later command should depend on the success or failure of an earlier one. A plain ampersand separates commands without making the second conditional.

  • command1 && command2 runs command2 only if command1 succeeds.
  • command1 || command2 runs command2 only if command1 fails.
  • command1 & command2 runs the commands in sequence without conditioning the second on the first command’s result.

A pipe sends one command’s output to another command, as in command1 | command2. Redirection saves output to a file: command > output.txt writes standard output to that file, replacing its existing contents. These operators are shell syntax, so quote or escape them if they must be treated as literal characters.

How do you keep environment changes inside a script?

Place setlocal near the beginning of a batch file when changes to variables, PATH, command extensions, or delayed expansion should not leak into the calling environment. endlocal restores the previous environment and shell settings; localization also ends when the batch file finishes. Scopes can be nested.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@echo off
setlocal
set "WORK_DIR=C:Batch Work"
rem Commands using WORK_DIR go here
endlocal

Use the quoted assignment form set "NAME=value" to avoid accidentally including a trailing space in the variable value. Microsoft’s setlocal reference notes a subtle exit-status effect: setlocal with an extensions or delayed-expansion option sets ERRORLEVEL to 0; without either option, it sets it to 1. Do not insert a setlocal command between a command whose exit status matters and the test for that status.

When do batch variables expand?

%NAME% substitutes a variable’s value when the command is processed. Expansion is context-sensitive in batch files, and it is not recursive: if one variable’s value contains text that looks like another variable reference, cmd does not repeatedly expand that substituted text as a second pass.

This timing can surprise you inside a parenthesized command block. A variable written as %NAME% may be expanded when the block is read, before commands inside it change the variable. Delayed expansion provides another way to read values as commands run: enable it with cmd /v:on or setlocal enabledelayedexpansion, then use !NAME!.

When delayed expansion is enabled, the exclamation mark becomes special syntax. Values containing literal ! can be altered by expansion, so avoid enabling it indiscriminately when processing arbitrary text or filenames. Choose the expansion method with the data and command context in mind.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do you check whether a command failed?

Test the exit status immediately after the command whose result matters. In batch syntax, if errorlevel N is true when the preceding program returned an exit code equal to or greater than N—not only when it returned exactly N. Microsoft documents this threshold behavior in its IF command reference.

@echo off
setlocal
backup.exe "C:Data"
if errorlevel 1 (
    echo Backup reported an error.
    exit /b 1
)
echo Backup completed successfully.

Here, any exit code of 1 or higher enters the error branch. If you need to distinguish specific nonzero values, account for the greater-than-or-equal comparison rather than treating if errorlevel N as an equality test. Another command can change the current error status, so test it before running anything that might overwrite it.

What limits and version scope should you know?

Microsoft’s cmd reference, updated May 23, 2025, lists support for Windows 10 and 11, Windows Server 2016, 2019, 2022, and 2025, and Azure Local 2311.2 and later. It documents a maximum individual environment-variable size of 8,192 bytes and a maximum total environment size of 65,536 characters per process, counting names, values, and equal signs. These are limits, not design targets.

For scripting needs beyond traditional batch capabilities, Microsoft’s reference says: “Users seeking more advanced capabilities are encouraged to explore PowerShell for enhanced scripting and automation.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.