How do I run two commands in one line in Windows CMD?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Use & to run two commands sequentially regardless of success or failure. Use && to run the second command only if the first succeeds. Use || to run the second command only if the first fails. These three operators cover every combination of conditional and unconditional chaining in Windows CMD.
The Three Operators
& (unconditional)
Runs the second command no matter what. The exit code of the first command is ignored.
Output:
Both commands run regardless of whether either succeeds or fails.
&& (run on success)
Runs the second command only if the first command returns exit code 0 (success).
If reports already exists, mkdir fails and the echo is skipped:
|| (run on failure)
Runs the second command only if the first command returns a non-zero exit code (failure).
If reports already exists:
Comparison Table
| Operator | Syntax | Second command runs when | Equivalent in Bash |
& | cmd1 & cmd2 | Always | ; or & |
&& | cmd1 && cmd2 | First succeeds (exit code 0) | && |
|| | cmd1 || cmd2 | First fails (non-zero exit code) | || |
| (pipe) | cmd1 | cmd2 | Always (stdout of cmd1 feeds cmd2) | | |
Chaining More Than Two Commands
You can chain as many commands as you need:
With conditional logic:
This only proceeds to the next step if the previous one succeeds. If cmake fails, make is never run.
Mixing operators
This reads as: try to create the folder. If it works, print success. If it fails, print the fallback message.
A more practical example:
Grouping Commands with Parentheses
Parentheses () group multiple commands into a single unit, which is essential when combining && and ||:
Without parentheses, only the last command before || is checked for failure. With parentheses, any failure inside the group triggers the fallback.
Multi-line grouping
In batch files, parentheses let you write grouped commands across multiple lines:
Using Pipes
Pipes (|) send the output of one command as input to the next:
This lists filenames in the current directory and filters for lines containing "report."
This counts the number of lines containing "ERROR" in a log file.
Pipe + conditional
>nul suppresses output. The find command's exit code drives the conditional.
Redirecting Output
Combine commands with output redirection:
| Redirect | Meaning |
> | Write stdout to file (overwrite) |
>> | Append stdout to file |
2> | Write stderr to file |
2>&1 | Redirect stderr to the same place as stdout |
>nul | Discard output |
2>nul | Discard errors |
Suppress all output
This silently creates the folder (or ignores the error if it exists) and always prints the message.
Practical Examples
Deploy a web project
Backup and compress
Check and create a directory
Run a server and open the browser
start opens the URL in the default browser. The & ensures node server.js runs immediately after (since start returns instantly).
Kill and restart a process
The first command kills any running Node process (silently ignoring errors if none is running), then starts the server.
Chain with timeout
System administration
The Semicolon Does NOT Work in CMD
Unlike Bash and PowerShell, CMD does not recognize ; as a command separator:
Output:
CMD treats everything after echo Hello as a single argument. Always use & in CMD.
PowerShell Equivalents
If you also use PowerShell, here is how the operators map:
| CMD | PowerShell | Behavior |
& | ; | Run sequentially, ignore exit code |
&& | && (PS 7+) or -and | Run on success |
|| | || (PS 7+) or -or | Run on failure |
| | | | Pipe (objects, not text) |
PowerShell 7+ supports && and || natively. Older PowerShell versions require -and / -or or if ($LASTEXITCODE -eq 0) checks.
Using These in Batch Files
Inside .bat or .cmd files, the same operators work. The main difference is that you can use call to invoke other batch files and still chain:
Without call, running a .bat file from another .bat transfers control and never returns. Always use call when chaining batch files.
Error handling pattern
Common Pitfalls
- Using
;instead of&. Semicolons are not command separators in CMD. They work in Bash and PowerShell 7+, but not in CMD. Always use&. - Forgetting
callbefore batch files. Runningsetup.bat && build.batinside a batch file will executesetup.batbut never return to runbuild.bat. Usecall setup.bat && call build.bat. - Operator precedence with
&&and||. When mixing operators, use parentheses to make intent explicit.a && b || cmeans "run b if a succeeds, otherwise run c." Without parentheses,a || b && cmay not behave as expected. ||catches all failures. If a command returns any non-zero exit code,||triggers. Some programs use non-zero codes for warnings, not errors. Check the specific program's exit code conventions.- Spaces around operators. Spaces are optional but recommended for readability.
dir&echo doneworks butdir & echo doneis clearer. - Long lines. CMD has a maximum command line length of 8191 characters. For complex chains, use a batch file with parenthesized groups instead.
Summary
&runs commands unconditionally in sequence. Use it when both commands should always run.&&runs the second command only on success. Use it for dependent operations likemkdir && cd.||runs the second command only on failure. Use it for error handling and fallbacks.- Use parentheses
()to group commands when mixing operators. - Use
callwhen chaining batch files inside a batch file. - Semicolons do not work as separators in CMD. Always use
&. - Pipes
|send the output of one command to the input of the next. - In PowerShell 7+,
&&and||work the same way. In older PowerShell, use;andif ($?).

