Introducing the Pipeline

The pipeline is a feature that allows the output of one command to be automatically transferred into another command. You can string any number of commands together and they will execute sequentially left to right. when it reaches the last command the results of this will be displayed on the screen or passed to a variable.

The simplest use of the pipleine is to format the output as a list or table

Get-Service | Format-List
Get-Service | Format-Table

You may see these shorterned to their alias fl or ft

Get-Service | ft 
Get-Service | fl

What this series of commands is doing is getting all services on the local machine and then formatting them into either a list or table. From left to right get-service then take the output and format-list

Using the pipeline

We can use the pipeline to automate tasks without knowing the list of items we need to change

Let’s consider a list of processes, we can find all running processes with a get command but which one? Lets use Get-Command to find it

Get-Command *process* returns a list of cmdlets we can use against processes, we can find them with Get-Process, stop them with Stop-Process and start them with Start-Process

We have a number of Notepad instances open on our machine and we want to stop the processes, how can we do that in one command? Lets start by finding all the running processes. I’ll start by looking at the syntax of the cmdlet using the help

we can see in the output of the Get-Process cmdlet that there is a column called processName so lets start with that can we list only the ones named ‘Notepad’

Get-Process -ProcessName 'Notepad'

It appears we can

now using the pipeline we can send this output to the stop-process command

Get-Process -ProcessName ‘Notepad’ | stop-process

This stops all running processes with the ProcessName ‘notepad’ – we can confirm this has worked by re-runnign the Get-Process command and checking their are none running

Other Uses

Other common commands we will pipe into frequently that we will look at in other posts

  • Sort-Object (alias sort)
  • Where-Object (alias where)
  • Filter-Object (alias filter)
  • Group-Object (alias group)
  • Format-Table (alias ft)
  • Format-List (alias fl)
  • Select-Object (alias select)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.