Get started Bring yourself up to speed with our introductory content.

Construct a chain of commands with the PowerShell pipeline

Administrators new to PowerShell can construct intricate workflows within a single line of code once they learn how to tap into the automation tool's piping abilities.

The ability to take output from one command and send it as input to the next command with the PowerShell pipeline is a major feature that sets PowerShell apart from other scripting languages. The pipeline links multiple commands to perform complex actions, such as configuration changes.

It's useful to think of PowerShell objects as analogous to a car, and the methods as the actions a car can take, such as moving forward or backward. Usually, an administrator would need a separate script for each method, but the PowerShell pipeline enables admins to consolidate those scripts and pass -- or pipe -- rich objects from one command to another.

The PowerShell pipeline helps condense code. For example, take the script below:

$service = Get-Service -Name XXX

$serviceName = $service.Name

Stop-Service -Name $serviceName

The administrator can shorten this code with the PowerShell pipeline to chain commands to pass the objects. The previous script is now one line of code that begins the task and stops it automatically when it completes:

Get-Service -Name XXX | Stop-Service

This video tutorial further explains what the PowerShell pipeline is and demonstrates how it works with real-life examples. 

The sample code

## The PowerShell Pipeline - Example
## Passing objects from the output of one command to the input of another

## Stopping a service
## NOT using the pipeline
Stop-Service -Name wuauserv
Remove-Item -Path 'C:\Computers.txt'

## Using the pipeline
Get-Service -Name wuauserv | Stop-Service
Get-Item -Path 'C:\Computers.txt' | Remove-Item

## Stopping a service and formatting the results using -PassThru (chaining commands)
Get-Service -Name wuauserv | Stop-Service -PassThru | Format-Table -AutoSize 

## Removing all BAK files in a folder
Get-ChildItem -Path C:\Folder -Filter '*.bak' | Remove-Item

View All Videos

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

Pipelines were one of the distinctive features of Unix tools when I started using them 40 years ago. And no, the Linux shell doesn't parse text output of a command that it's piping to another command (unless you want to use the shell's parsing capabilities, of course.)