Windows CMD
Command Line
Run Multiple Commands
Techie Tips
Operating System Hacks

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.

cmd
echo Starting & echo Finished

Output:

 
Starting
Finished

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).

cmd
mkdir reports && echo "Folder created successfully"

If reports already exists, mkdir fails and the echo is skipped:

 
A subdirectory or file reports already exists.

|| (run on failure)

Runs the second command only if the first command returns a non-zero exit code (failure).

cmd
mkdir reports || echo "Folder already exists, continuing..."

If reports already exists:

 
A subdirectory or file reports already exists.
Folder already exists, continuing...

Comparison Table

OperatorSyntaxSecond command runs whenEquivalent in Bash
&cmd1 & cmd2Always; or &
&&cmd1 && cmd2First succeeds (exit code 0)&&
||cmd1 || cmd2First fails (non-zero exit code)||
| (pipe)cmd1 | cmd2Always (stdout of cmd1 feeds cmd2)|

Chaining More Than Two Commands

You can chain as many commands as you need:

cmd
echo Step 1 & echo Step 2 & echo Step 3 & echo Done

With conditional logic:

cmd
mkdir build && cd build && cmake .. && make

This only proceeds to the next step if the previous one succeeds. If cmake fails, make is never run.

Mixing operators

cmd
mkdir output && echo "Created output folder" || echo "output folder already exists"

This reads as: try to create the folder. If it works, print success. If it fails, print the fallback message.

A more practical example:

cmd
ping -n 1 google.com >nul && echo "Internet is up" || echo "No internet connection"

Grouping Commands with Parentheses

Parentheses () group multiple commands into a single unit, which is essential when combining && and ||:

cmd
(cd project && npm install && npm run build) || echo "Build failed"

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:

batch
1@echo off
2(
3    cd project
4    npm install
5    npm run build
6) && (
7    echo Build succeeded
8    echo Deploying...
9    deploy.bat
10) || (
11    echo Build failed
12    echo Check the logs
13    exit /b 1
14)

Using Pipes

Pipes (|) send the output of one command as input to the next:

cmd
dir /b | find "report"

This lists filenames in the current directory and filters for lines containing "report."

cmd
type server.log | find /c "ERROR"

This counts the number of lines containing "ERROR" in a log file.

Pipe + conditional

cmd
dir /b | find "config.json" >nul && echo "Config found" || echo "Config missing"

>nul suppresses output. The find command's exit code drives the conditional.

Redirecting Output

Combine commands with output redirection:

cmd
echo Starting deployment... >> deploy.log & deploy.bat >> deploy.log 2>&1
RedirectMeaning
>Write stdout to file (overwrite)
>>Append stdout to file
2>Write stderr to file
2>&1Redirect stderr to the same place as stdout
>nulDiscard output
2>nulDiscard errors

Suppress all output

cmd
mkdir temp >nul 2>&1 & echo "Continuing regardless"

This silently creates the folder (or ignores the error if it exists) and always prints the message.

Practical Examples

Deploy a web project

cmd
cd C:\project && git pull && npm install && npm run build && echo "Deploy complete"

Backup and compress

cmd
xcopy C:\data D:\backup\data /E /I /Y && echo Backup done || echo Backup failed

Check and create a directory

cmd
if not exist "C:\logs" mkdir "C:\logs" & echo Ready >> C:\logs\status.txt

Run a server and open the browser

cmd
start http://localhost:3000 & node server.js

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

cmd
taskkill /F /IM node.exe >nul 2>&1 & node server.js

The first command kills any running Node process (silently ignoring errors if none is running), then starts the server.

Chain with timeout

cmd
echo "Waiting 5 seconds..." & timeout /t 5 /nobreak >nul & echo "Resuming"

System administration

cmd
net stop "MySQL" && net start "MySQL" && echo "MySQL restarted" || echo "Restart failed"

The Semicolon Does NOT Work in CMD

Unlike Bash and PowerShell, CMD does not recognize ; as a command separator:

cmd
echo Hello; echo World

Output:

 
Hello; echo World

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:

CMDPowerShellBehavior
&;Run sequentially, ignore exit code
&&&& (PS 7+) or -andRun on success
|||| (PS 7+) or -orRun on failure
||Pipe (objects, not text)

PowerShell 7+ supports && and || natively. Older PowerShell versions require -and / -or or if ($LASTEXITCODE -eq 0) checks.

powershell
1# PowerShell 7+
2mkdir reports && Write-Host "Created" || Write-Host "Already exists"
3
4# PowerShell 5.1 (no && support)
5mkdir reports; if ($?) { Write-Host "Created" } else { Write-Host "Already exists" }

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:

batch
1@echo off
2call setup.bat && call build.bat && call deploy.bat || (
3    echo Deployment pipeline failed
4    exit /b 1
5)

Without call, running a .bat file from another .bat transfers control and never returns. Always use call when chaining batch files.

Error handling pattern

batch
1@echo off
2setlocal
3
4call :step "Checking prerequisites" check_prereqs.bat
5call :step "Building project" build.bat
6call :step "Running tests" test.bat
7call :step "Deploying" deploy.bat
8
9echo All steps completed successfully
10exit /b 0
11
12:step
13echo [%~1]
14call %~2
15if errorlevel 1 (
16    echo FAILED: %~1
17    exit /b 1
18)
19goto :eof

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 call before batch files. Running setup.bat && build.bat inside a batch file will execute setup.bat but never return to run build.bat. Use call setup.bat && call build.bat.
  • Operator precedence with && and ||. When mixing operators, use parentheses to make intent explicit. a && b || c means "run b if a succeeds, otherwise run c." Without parentheses, a || b && c may 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 done works but dir & echo done is 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 like mkdir && cd.
  • || runs the second command only on failure. Use it for error handling and fallbacks.
  • Use parentheses () to group commands when mixing operators.
  • Use call when 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 ; and if ($?).

Course illustration
Course illustration

All Rights Reserved.