Pipes
A pipe | connects the standard output of one command to the standard input of the next. Instead of saving results in a temporary file and reading it back, you build a pipeline: each command does one small job and passes its text along.
dir /b /o:n | findstr /i "report" | sort /r
─────────── ────────────────── ───────
list names ──► keep matching lines ──► reverse order ──► consoleA first pipeline
@echo off
mkdir demo & cd demo
for %%n in (report-jan.xlsx notes.txt report-feb.xlsx photo.jpg) do echo x> %%n
echo --- all files:
dir /b /o:n
echo --- only reports, newest month first:
dir /b /o:n | findstr "report" | sort /r--- all files:
notes.txt
photo.jpg
report-feb.xlsx
report-jan.xlsx
--- only reports, newest month first:
report-jan.xlsx
report-feb.xlsx
dir /b produced four names, findstr kept the two containing report, and sort /r reversed them. "jan" sorts after "feb" alphabetically, which is why it came first: sort compares text, not dates. Text is all that flows through a CMD pipe; PowerShell pipes pass objects instead (see PowerShell Pipeline).
Commands that read from a pipe
Only commands that read standard input do something useful on the right side of |:
| Command | What it does with piped text |
|---|---|
findstr |
keeps lines matching a pattern (findstr) |
find |
keeps lines containing a string, or counts them (find & sort) |
sort |
sorts the lines |
more |
shows the text one screenful at a time |
clip |
copies the text to the Windows clipboard |
for /f |
runs a command for each line (with 'command' in single quotes) |
Commands such as echo, dir, copy or type ignore their input, so dir | type doesn't do anything useful.
Filtering and sorting text
@echo off
mkdir demo & cd demo
(
echo 2026-10-01 INFO service started
echo 2026-10-01 ERROR disk quota exceeded
echo 2026-10-02 INFO backup done
echo 2026-10-02 ERROR cannot reach db01
echo 2026-10-03 WARN slow response
) > app.log
echo --- errors:
type app.log | findstr "ERROR"
echo --- errors and warnings, sorted by message:
type app.log | findstr "ERROR WARN" | sort /+18--- errors:
2026-10-01 ERROR disk quota exceeded
2026-10-02 ERROR cannot reach db01
--- errors and warnings, sorted by message:
2026-10-02 ERROR cannot reach db01
2026-10-01 ERROR disk quota exceeded
2026-10-03 WARN slow response
findstr "ERROR WARN" keeps lines containing either word, and sort /+18 sorts by the text starting at column 18, the message. findstr and find can also read a file directly (findstr "ERROR" app.log); type … | is used here to show the pipe.
Pipes and errors
Only stdout goes into a pipe; error messages still go to the console. To send errors through the pipe as well, redirect stream 2 to stream 1 before the pipe:
@echo off
(
echo c: ok
>&2 echo a: FAILED
echo b: ok
) 2>&1 | sorta: FAILED
b: ok
c: ok
Without 2>&1, the line a: FAILED would appear on the console directly and sort would only see the two ok lines.
Handy pipes with more and clip
These two are interactive, so they are shown as plain sessions:
C:\Users\ada>dir /s /b C:\Windows\System32\*.dll | more
C:\Users\ada>ipconfig | findstr /i "IPv4"
IPv4 Address. . . . . . . . . . . : 192.168.1.42
C:\Users\ada>dir /b | clipThe second line is sample output; your address will differ. The last command puts the file list on the clipboard, ready to paste into an email or a document.
Each side of a pipe runs in a new CMD
Here is the catch that surprises everyone eventually: when a built-in command or a block is part of a pipe, CMD starts a separate cmd.exe process to run it. Variables set there vanish when that process ends:
@echo off
set "name=before"
echo after| set /p name=
echo name is still: %name%
echo after> tmp.txt
set /p name=< tmp.txt
echo name is now: %name%
del tmp.txtname is still: before
name is now: after
set /p did read after, but in a child process that exited immediately. Reading from a file with < happens in the current CMD, so the value survives. To capture a command's output in a variable, use for /f (for /f: Parse Text):
@echo off
mkdir demo & cd demo
for %%n in (a.txt b.txt c.log) do echo x> %%n
for /f %%c in ('dir /b /o:n ^| findstr ".txt"') do echo found %%cfound a.txt
found b.txt
Inside for /f ('…'), the pipe character must be escaped as ^|, otherwise the outer CMD would try to use it.
Pipes vs. && and redirection
| Symbol | Connects | Example |
|---|---|---|
| |
output of A → input of B | dir /b | sort |
> / < |
a command ↔ a file | sort < in.txt > out.txt |
&& / || |
exit code of A → whether B runs | mkdir x && cd x |
& |
nothing, just sequence | cls & dir |
Learn more: Pipes in every shell · Bash Pipes & Redirection
Test yourself
What does | pass from the left command to the right one?
- Its exit code
- Its standard output
- Its error messages
- Its variables
Why does echo hi| set /p v= not change %v% in the script?
- set /p can't read input
- The right side of the pipe runs in a separate cmd.exe process
- echo adds quotes
- Variables can't hold text
How must a pipe be written inside for /f %%x in ('...')?
- |
- ^|
- \|
- ||