Jason Turley's Website

PowerShell versus Linux Pipes

I am a PowerShell noob who’s more familiar with programming in Linux. (Ok, technically it’s GNU/Linux, but for simplicity’s sake I’m going to use Linux to refer to GNU/Linux, Unix, BSD, etc.) 

Since I learned Linux first, this was my baseline for understanding what pipes are and how to use them. So when I began learning PowerShell, I was confused when pipes behaved differently. 

This is a very short guide for anyone who wants a quick rundown of using pipes in Linux vs PowerShell. 

Wait, so what’s a pipe anyways?

Simply put, a pipe is a way to redirect the output of one thing into the input of another thing. They are used to change the “flow” of information and can be chained together. 

This is similar to how a real-world pipe redirects the flow of water from one end to another.

The syntax for a pipe is the vertical bar | above the \ key.

Linux pipes

In Linux, pipes are used to redirect the output of one process into the input of another. This output is treated as text, and thus can be easily manipulated and redirected. 

In the below example, ls prints all files in the current directory. The output is piped to grep, which searches for all files ending in .txt. As a result, only .txt files are printed to the screen:

This example prints all running processes with ps, searches for ones containing the word “init” with grep, and writes the output the screen and a file with tee:

PowerShell pipes

In PowerShell, pipes are used to redirect objects, not text, between cmdlets. This is an important distinction from Linux pipes. For this reason many Linux use cases will not work in PowerShell. 

In the following example the cmdlet Get-ChildItem displays the contents of the current directory, and uses Sort-Object to sort them from newest to oldest: 


No one pipe environment is better than the other. They both have their own strengths. However, if I had to choose between Linux and PowerShell to teach pipes to a beginner, I would choose Linux.

To me they are just simpler to use and understand. The text-based output is more intuitive to handle than object-based output.

For PowerShell, it took some trial-and-error to get even the simplest pipeline commands to work.

#linux #windows