Pipes and Filters

Maintained on

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"
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>tasklist | findstr "notepad"
notepad.exe 12345 Console 1 15,432 K
C:\users\user>

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.

CommandDescription
findExtracts lines containing a given string
findstrAdvanced string search with regular expressions
sortSorts text in ascending or descending order
moreDisplays output one screen at a time

Filtering with find

dir | find "txt"
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir | find "txt"
03/10/2026 02:30 PM 1,234 report.txt
03/11/2026 09:15 AM 567 memo.txt
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

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

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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

Counting lines

find /C counts the matching lines.

dir /b *.txt | find /C /V ""
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /b *.txt | find /C /V ""
5
C:\users\user>
チェック

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"
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>netstat -an | findstr "ESTABLISHED LISTENING"
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING
TCP 192.168.1.100:54321 172.16.0.1:443 ESTABLISHED
C:\users\user>
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
OptionDescription
/RSort in descending (reverse) order
/+nSort starting from the n-th character

Display PATH one entry per line

(for %i in ("%PATH:;=" "%") do @echo %~i) | sort
Using this in a batch file

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:

  1. tasklist gets the process list
  2. findstr extracts lines containing “chrome”
  3. sort sorts the result
  4. more displays 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"
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>netstat -ano | findstr ":8080"
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 12345
C:\users\user>

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
Pipe vs. redirection

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.exe instances, so variables set with set inside a pipe are not carried over.
  • When using a pipe inside the in ('...') part of FOR /F, escape it as ^|.

Summary

TaskExample
Filter by stringcommand | find "string"
Filter by regexcommand | findstr "pattern"
Sortcommand | sort
Page the outputcommand | more
Count linescommand | find /C /V ""
Chain multiple filterscommand | findstr "A" | sort

Quiz

Practice Problem

Which statement correctly describes the pipe in command1 | command2?

回答がサーバーに送信されることはありません
#Command Prompt #Batch File #Pipe #Filter #Command