Use this when cmd commands or batch scripts misbehave.
CMD Troubleshooting
Troubleshoot cmd and batch scripts with echo on, checking %ERRORLEVEL% after each command, quoting paths with spaces, enabling delayed expansion (!var!) inside loops and running as the right user or elevated.
1. Exact error text and %ERRORLEVEL%.
2. where <cmd> — command found?
3. Quoting: paths with spaces in double quotes.
4. Variables expanded at parse time → delayed expansion in blocks.
5. Compare as admin vs normal user.
line parsed → expansion → command resolution → execution → error code
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Known-good, broken and when it is fixed Verify boundary
Known-good is the exact command or batch file executing under the intended account, working directory and environment, resolving the intended executable and returning the expected exit code/output. Broken evidence includes divergent PATH/current directory, wrong variable expansion, parse errors or an unexpected nonzero exit code at the first failing stage. Verify with the same invocation mechanism used by the workload, not only a manually opened elevated prompt. A successful cmd.exe launch shows only the interpreter started, but it does not show the script parsed as intended or that downstream tools completed successfully.
Step 2 Common mistakes Narrow the fault
- Mixing CMD and PowerShell syntax.
Step 3 Native failure isolation and next-check logic Narrow the fault
If a command is 'not recognized', resolve whether the executable is absent, PATH differs, App Paths/alias behavior was assumed, or quoting split the path. If arguments are wrong, echo or otherwise inspect the expanded variables and isolate metacharacters such as &, |, >, <, ^ and parentheses. If a batch file works interactively but fails under Scheduler/service context, compare working directory, account, environment and noninteractive input. When a pipeline fails, test each stage and record ERRORLEVEL immediately because a later command can overwrite it. Prefer a minimal reproduction with the same interpreter and environment over adding more escaping blindly.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Unquoted paths with spaces break commands.
- %var% inside a block expands once when the block is parsed.
- Some commands need an elevated prompt.
echo %PATH%— PATH in CMDset— environment variableswhere <name>— command resolutionecho %ERRORLEVEL%— exit code
ReferenceHow it works1 section · depth on demand
How it works
CMD troubleshooting is usually about parsing, environment and process context rather than the command interpreter being generically 'broken'. cmd.exe expands variables, applies quoting and metacharacter rules, resolves executables through the current directory and PATH, performs redirection/pipeline setup, and propagates exit codes between commands and batch files. Interactive and batch parsing differ around percent expansion, delayed expansion and nested quoting. The working directory, inherited environment, code page and 32/64-bit process context can change behavior. Preserve the literal command line, current directory, relevant variables and exit code before rewriting a script.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Quoting with cmd /c — the rules that bite
The cmd reference documents that /s strips the first and last quote of the command string, and that without /s the quotes are preserved only when exactly one pair is used, there are no special characters (& < > ( ) @ ^ |) inside, the text contains whitespace, and it names an executable. Otherwise the first and last quotes are removed.
cmd /c ""C:\Program Files\Tool\tool.exe" --out "C:\Temp\my file.txt""
The outer pair is sacrificed; the inner quotes survive. This is the reliable form when calling cmd from schedulers or other programs.
Delayed expansion
@echo off
setlocal EnableDelayedExpansion
set count=0
for %%f in (*.log) do (
set /a count+=1
echo !count! %%f
)
echo Total: %count%
Inside a parenthesised block %count% is expanded once when the block is parsed; !count! is expanded at run time. The cmd reference covers /v:on and the ! syntax.
Error handling
| Construct | Behaviour |
|---|---|
command && echo ok | Runs second only if first returns 0 |
command \|\| echo failed | Runs second only on non-zero |
if errorlevel 1 | True for exit codes ≥ 1 |
if %errorlevel% neq 0 | Exact comparison |
exit /b 3 | Return code from a batch file or subroutine |
Common batch problems
| Symptom | Cause |
|---|---|
| Script works interactively, fails as a scheduled task | Working directory is C:\Windows\System32; use cd /d "%~dp0" |
%~dp0 vs %cd% confusion | %~dp0 is the script's folder; %cd% the current directory |
Paths with & or ^ break commands | Quote them or escape with ^ |
| UNC path as current directory unsupported | Use pushd \\server\share (maps a temporary drive) |
| Unicode file names garbled | chcp 65001 and save scripts in UTF-8 (without BOM) where supported |
% signs in arguments disappear | Double them in batch files (%%) |
For anything beyond simple glue, PowerShell gives structured errors and objects; keep cmd scripts short.
Method
CMD troubleshooting covers batch scripts and command-line tools that fail or behave unexpectedly.
| Problem | Check |
|---|---|
| Command not recognised | PATH, file extension, typos; use full path |
| Script stops or continues unexpectedly | %ERRORLEVEL% handling, if errorlevel semantics |
| Variables not updating in loops | Use setlocal enabledelayedexpansion and !var! |
| Paths with spaces fail | Quote paths |
| Works interactively, fails as scheduled task | Working directory, mapped drives, user context |
| Access denied | Run as administrator; UAC |
@echo on
set
where robocopy
yourcommand.exe & echo exit=%ERRORLEVEL%
pushd "%~dp0" :: run relative to the script's folder
For anything beyond simple tasks, PowerShell offers better error handling and is easier to troubleshoot.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Command Prompt → CMD Troubleshooting. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands · confidence 94%