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
Cloud Computing
Enterprise Desktop
Virtual Desktop
Close