Understand The Power Of Windows PowerShell Pipeline ( | )

PowerShell Pipeline ( | ) 

Windows PowerShell pipeline allows us to join two or more statements with a pipe symbol, sometimes called the '|' bar key. This tells PowerShell that we want to take the output of one command and pass it as the input to the next command. Let’s look at a simple example to better understand the power of Pipeline in Windows PowerShell.

Pipeline Example in Windows PowerShell

List all Services

The below Cmdlet lists all the services.

PS C:\ > Get-Service

Windows PowerShell

We can clearly see the output of Get-Service. It returned all the services including stopped services. Let’s take ADWS service as an example.

Display ADWS service

To display ADWS service, we just need to specify it using the -Name parameter.

PS C: \ > Get-Service -Name ADWS

Windows PowerShell

Note

As we can see the above results, ADWS service is currently Running.

Stop ADWS service

To stop ADWS service, we use Stop-service Cmdlet along with a -Name parameter to specify the ADWS Service name.

PS C:\ > Stop-Service -Name ADWS

Windows PowerShell

Once we run the above Cmdlet, we can go back and verify the status of ADWS service.

Verify ADWS Service Status

PS C:\ > Get-Service -Name ADWS

Windows PowerShell

From the screenshot, we can confirm that we successfully stopped ADWS service. This is one way of stopping a service. But there’s an efficient way to do this using PowerShell Pipeline ( | ). Let’s understand how we can use the Pipleline ( | ) to do this in a better and efficient way.

Understanding the Power of Pipeline in PowerShell

PowerShell Pipeline ( | ) takes output from one object and pass it as input to another object. The below examples explain the power of PowerShell Pipeline.

Example

PS C:\ > Get-Service -Name ADWS | Stop-Service

Windows PowerShell

Important Note 

The above Cmdlet performs two tasks. In the first task ( Get-Service ), it gets the ADWS service from the list of services, then using the Pipe ( | ) symbol, it passes the output of the Get-Service as an input to Stop-Service. So, the second task ( Stop-Service ) will stop ADWS service.


Similar Articles