Pipes and Filters
In Command Prompt, you can pass the output of one command to another command as its input. This mechanism is called a pipe.
With pipes, you can easily filter, sort, and page through large amounts of output.
Pipe Basics
A pipe is written with | (vertical bar).
command1 | command2
The standard output of command1 is passed to command2 as its standard input.
Basic example
tasklist | findstr "notepad"
The output of tasklist is passed straight to findstr, which shows only the lines containing “notepad”.
Common Filter Commands
Commands used together with pipes are called filter commands.
| Command | Description |
|---|---|
find | Extracts lines containing a given string |
findstr | Advanced string search with regular expressions |
sort | Sorts text in ascending or descending order |
more | Displays output one screen at a time |
Filtering with find
dir | find "txt"

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Counting lines
find /C counts the matching lines.
dir /b *.txt | find /C /V ""
find /C /V "" counts the lines that do not contain an empty string, which in effect returns the number of all lines. It is handy for counting the lines of piped output.
Filtering with findstr
findstr supports regular expressions and multiple conditions.
netstat -an | findstr "ESTABLISHED LISTENING"
tasklist | findstr /V "svchost"
The /V option shows only lines that do not contain “svchost”. Note that space-separated strings such as findstr "A B" mean “A or B”. To search for a string that contains a space, use findstr /C:"A B".
Sorting with sort
The sort command sorts output in ascending or descending order.
dir /b | sort
dir /b | sort /R
| Option | Description |
|---|---|
/R | Sort in descending (reverse) order |
/+n | Sort starting from the n-th character |
Display PATH one entry per line
(for %i in ("%PATH:;=" "%") do @echo %~i) | sort
The example above is for typing directly at the prompt. In a batch file, change %i to %%i.
Paging with more
When the output is too long for the screen, more shows it page by page.
dir /s | more
type longfile.txt | more
Press the space bar for the next page and Q to quit.
Chaining Pipes
You can chain pipes more than once.
tasklist | findstr /I "chrome" | sort | more
This does the following in order:
tasklistgets the process listfindstrextracts lines containing “chrome”sortsorts the resultmoredisplays it one screen at a time
Practical Examples
Count running processes
tasklist | find /C /V ""
Find the process using a specific port
netstat -ano | findstr ":8080"
The last column (12345) is the process ID. Use tasklist /FI "PID eq 12345" to see details.
Check the total size of files in a folder
dir /s /-c | findstr "File(s)"
This shows a summary line for each subfolder and a final total. The matched text depends on the OS display language; on a Japanese system, use "個のファイル" instead.
Filter output and save it to a file
You can also combine pipes and redirection.
tasklist | findstr "chrome" > chrome_processes.txt
A pipe | passes a command’s output to another command as input. Redirection > saves a command’s output to a file. Combining both lets you write filtered results to a file.
Things to Keep in Mind
- Only standard output goes through the pipe. To pass error messages too, use
command 2>&1 | findstr "...". - The commands on each side of a pipe run in separate
cmd.exeinstances, so variables set withsetinside a pipe are not carried over. - When using a pipe inside the
in ('...')part ofFOR /F, escape it as^|.
Summary
| Task | Example |
|---|---|
| Filter by string | command | find "string" |
| Filter by regex | command | findstr "pattern" |
| Sort | command | sort |
| Page the output | command | more |
| Count lines | command | find /C /V "" |
| Chain multiple filters | command | findstr "A" | sort |