Powershell

What is a pipe in PowerShell?

PowerShell provides a very useful operator named Pipeline operator. While working with PowerShell, it is necessary to know when and how to use pipelines. In PowerShell, the pipeline operator is used to do things faster and more efficiently. Using the pipeline is easy, we have to place the command we want to run in the pipe symbol and pipe it to the next command.

This post will explore the below-listed aspects of the PowerShell pipeline:

So, let’s begin!

What is a PowerShell Pipeline?

In PowerShell, an operator named “Pipeline” is used to connect the multiple/series of commands. The pipeline operator is denoted by the “|” sign. In PowerShell, the pipeline operator accepts the output of one cmdlet and sends it as input to the next cmdlet.

Basic Syntax

The below code snippet will assist you with a basic understanding of PowerShell Pipeline:

cmdlet1 | cmdlet2 | cmdlet3 | ....

In this way, we can specify as many cmdlets as needed, and the pipeline operator “|” will Join every single cmdlet with other cmdlets.

How Pipeline Operator Works in PowerShell

In this section we will explain the working of pipeline operators with the help of some examples.

Stop a process using pipeline in PowerShell
Let’s run the Get-process cmdlet to see the list of processes running on our machine:

Let’s say we want to stop the “Calculator” process using the pipeline operator. To do that, we will run the below-given command in PowerShell:

Get-Process -Name Calculator | Stop-Process

Here, in this example program, firstly, the Get-process cmdlet will get the program named “Calculator” and assign the resultant output to the Stop-process cmdlet as input using pipeline operator:

From the above snippet, we observed that the cursor moved to the next line without showing any error, it verifies that the specified process has been stopped. We can also verify it from the list of running processes:

The above snippet verified that the “Calculator” process is not in the list of running processes.

Show the last three processes that are consuming CPU Cycles
The below example program will utilize multiple pipeline operators:

Get-Process |
  Where-Object CPU -gt 0 |
    Sort-Object WorkingSet -Descending |
      Select-Object -Last 3
  • The Get-process cmdlet will return all of the processes on our local system as the process object.
  • These process objects will be passed to the Where-object cmdlet as an input using the pipeline operator while the Where-Object command will filter all the objects that are consuming the CPU.
  • The output of the Where-Object cmdlet will be piped to the Sort-Object cmdlet which will sort the objects in descending order.
  • Finally, the output of Sort-object will be piped to the Select-Object cmdlet which will give us the last five processes:

The above-snippet verified the working of the pipe operator in PowerShell.

Show the top three processes that are consuming the CPU
In this example program, we will utilize multiple commands using the pipeline operator to find which three processes are consuming the maximum CPU:

Get-Process |
  Where-Object CPU -gt 0 |
    Sort-Object WorkingSet -Descending |
      Select-Object -First 3

This time we utilized “-First 3” instead of “-Last 3”, as a result, we will get the list of top three processes that are consuming CPU cycles:

This is how the pipeline operator works in PowerShell.

Conclusion

The pipeline operator or the pipe operator, is used when we have to combine several commands into one output. The pipeline operator in PowerShell accepts the output of one cmdlet and sends it as input to the next cmdlet. This tutorial explained what a pipeline is, its basic syntax, and how it works in PowerShell.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.