PowerShell: Pipe Output

By Xah Lee. Date: . Last updated: .

You can send one command's output to another command's input. This is called piping. you write it as: command1 | command2 | command3.

Here are some examples:

# list current dir, sort it
dir | sort

# list, sort, show first 5 elements
dir | sort | select -first 5

# list, sort, select, then format for display
dir | sort | select -first 5 | format-table name, length
# move jpg files to another dir
Get-Item $home/Documents/*jpg | mv -Destination $home/Pictures/
# filter out bot access from web log, save to new file
get-content weblog.txt |
select-string -notmatch "Googlebot" |
out-file -Encoding utf8 -width 999000 weblog2.txt

How Pipe Works

Certain commands are almost always used with pipeline, such as

PowerShell pipeline input 2021-05-23
help name showing a parameter accept pipeline input.

See also: PowerShell: Object Type, Properties, Methods




Script Examples