ZetCode

Windows Command Prompt Chained Commands

last modified July 14, 2025

Chained commands in Windows Command Prompt allow executing multiple commands in sequence or conditionally. They enable complex operations without writing separate scripts. Command chaining uses special operators to control command execution flow. This technique is essential for efficient command-line work.

The command chaining operators include &, &&, ||, and |. Each serves a different purpose in combining commands. Understanding these operators greatly enhances command-line productivity. They can be used in both interactive sessions and batch files.

Chained commands reduce the need for temporary files and intermediate steps. They allow filtering, processing, and acting on command outputs directly. Mastering command chaining is a key skill for Windows system administrators. This tutorial covers all major command chaining techniques with examples.

Basic Definitions

Command chaining operators control how multiple commands execute:

These operators can be combined to create complex command sequences. They work in both interactive cmd sessions and batch files.

Sequential Execution with &

The & operator runs multiple commands sequentially. All commands execute regardless of whether previous commands succeed or fail.

sequential.bat
@echo off
echo First command & echo Second command & echo Third command

This example demonstrates simple sequential execution. All three echo commands will run one after another.

echo First command & echo Second command & echo Third command

The & operator connects three echo commands. Each command runs after the previous one completes, regardless of success.

C:\Users\Jano>sequential.bat
First command
Second command
Third command

The output shows all commands executed in order. This is useful when you need to run several independent commands together.

Conditional Execution with &&

The && operator provides conditional execution. The second command runs only if the first command succeeds (returns errorlevel 0).

conditional.bat
@echo off
dir C:\Windows\System32\cmd.exe && echo CMD found successfully
dir C:\Windows\System32\nonexistent.txt && echo This won't display

This script shows successful and failed conditional execution. The second echo only runs if the dir command finds the file.

dir C:\Windows\System32\cmd.exe && echo CMD found successfully

Since cmd.exe exists, the dir succeeds and the echo command executes. This is useful for success notifications.

dir C:\Windows\System32\nonexistent.txt && echo This won't display

The dir fails to find nonexistent.txt, so the echo doesn't run. && prevents further execution on failure.

C:\Users\Jano>conditional.bat
 Volume in drive C is Windows
 Volume Serial Number is 4415-13BB

 Directory of C:\Windows\System32

09. 07. 2025  10:30           376 832 cmd.exe
               1 File(s)        376 832 bytes
               0 Dir(s)  10 326 323 200 bytes free
CMD found successfully
 Volume in drive C is Windows
 Volume Serial Number is 4415-13BB

 Directory of C:\Windows\System32

File Not Found

Output shows only successful commands after && execute. The second echo is skipped due to the failed dir command.

Error Handling with ||

The || operator executes the second command only if the first fails. It's useful for error handling and fallback operations.

doping.bat
ping example.com -n 1 >nul || echo Unable to reach server!

This script attempts to ping a server. If the ping fails, it displays an error message.

Command Piping with |

The | operator pipes output from one command to another. This enables powerful command combinations and output processing.

piping.bat
@echo off
echo Getting system info...
systeminfo | findstr /i "OS Name"

echo Checking if Chrome is running...
tasklist | findstr /i "chrome" || echo Chrome not running.

echo Saving network info to file...
ipconfig | findstr /i "IPv4" > network_info.txt
type network_info.txt

This script shows practical uses of command piping. The | operator connects commands where the first's output becomes the second's input.

systeminfo | findstr /i "OS Name"

Pipes system information output to findstr, filtering for just the OS name line. This extracts specific information from verbose output.

tasklist | findstr /i "chrome"

Lists all running processes and filters for Chrome instances. Useful for checking if specific applications are running.

ipconfig | findstr /i "IPv4" > network_info.txt

Gets network configuration, filters for IPv4 addresses, and saves to a file. Combines piping with output redirection.

Combining Operators

Chaining operators can be combined for complex logic. This example shows mixed usage of &, &&, ||, and |.

combined.bat
@echo off
echo Starting system checks...
echo ----------------------------

:: Get OS Name
for /f "tokens=2 delims=:" %%A in ('systeminfo ^| findstr /i "OS Name"') do (
    echo OS Name:%%A
)

:: Check if kernel32.dll exists
if exist C:\Windows\System32\kernel32.dll (
    echo Found kernel32.dll

    :: Get DLL version using PowerShell
    for /f "delims=" %%V in ('powershell -command "(Get-Item 'C:\Windows\System32\kernel32.dll').VersionInfo.FileVersion"') do (
        echo kernel32.dll Version: %%V
    )
) else (
    echo DLL not found
)

This script demonstrates advanced command chaining. It combines multiple operators for conditional execution and error handling.

:: Get OS Name
for /f "tokens=2 delims=:" %%A in ('systeminfo ^| findstr /i "OS Name"') do (
    echo OS Name:%%A
)

Uses a for loop to extract the OS name from systeminfo output. The pipe (|) operator filters the output with findstr, and the for loop processes it.

:: Check if kernel32.dll exists
if exist C:\Windows\System32\kernel32.dll (
    echo Found kernel32.dll

    :: Get DLL version using PowerShell
    for /f "delims=" %%V in ('powershell -command "(Get-Item 'C:\Windows\System32\kernel32.dll').VersionInfo.FileVersion"') do (
        echo kernel32.dll Version: %%V
    )
) else (
    echo DLL not found
)

Checks for the existence of kernel32.dll. If it exists, it retrieves the version using PowerShell. The if exist command conditionally executes the PowerShell command to get the DLL version.

Source

Command Redirection Operators

This tutorial covered Windows Command Prompt chained commands. Mastering these techniques enables powerful command-line automation and scripting.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.