PowerShell

PowerShell Pipelines Explained for Bash Users

By WickedScript Editorial Team · · 4 min read

If you are comfortable with Bash, PowerShell can feel familiar and strange at the same time. It has a pipe character, it has commands that you chain, and it runs in a terminal. But the pipe works differently, and understanding that one difference explains most of the rest of the language.

In Bash, a pipeline passes plain text from one program to the next. In PowerShell, a pipeline passes objects: structured values with named properties. This article compares the two approaches and shows how common Bash habits translate.

Text versus objects

When you run ls -l | grep ".log" in Bash, ls prints lines of text, and grep searches those lines. If you then want the file size, you cut a column out of the text, and you hope the column layout stays the same.

In PowerShell, the equivalent command emits file objects. Each object already has properties such as Name, Length and LastWriteTime. The next command in the pipeline works with those properties directly, so there is no need to split or parse anything.

Get-ChildItem | Where-Object { $_.Extension -eq ".log" }

Here $_ stands for the current object flowing through the pipe, and .Extension reads one of its properties. The advantage is reliability: your pipeline does not break when a column shifts or a file name contains spaces.

Naming and the Verb-Noun pattern

PowerShell commands are called cmdlets, and they follow a Verb-Noun pattern such as Get-Process, Stop-Service and Set-Content. It is wordier than ps or cat, but it is predictable. Once you know the verbs Get, Set, New, Remove and Start, you can often guess a command name.

Many familiar names exist as aliases, so ls, cat, cd and pwd work on the PowerShell prompt. Be careful, though: these aliases map to PowerShell cmdlets, not to the Unix programs. They accept different options, so ls -l does not behave the way you expect.

Translating common Bash tools

Here is a rough map from text tools to PowerShell cmdlets.

  • grep becomes Where-Object for property tests, or Select-String for searching text in files.
  • cut and awk '{print $(1)}' become Select-Object with the property names you want.
  • sort becomes Sort-Object, which can sort by a property and has a -Descending switch.
  • uniq -c becomes Group-Object.
  • wc -l becomes Measure-Object.
  • head and tail become Select-Object -First and Select-Object -Last.
  • xargs often becomes ForEach-Object.

Putting several together, this command lists the five largest files in a folder.

Get-ChildItem -File |
  Sort-Object Length -Descending |
  Select-Object -First 5 Name, Length

A trailing pipe character at the end of a line lets you continue on the next line, which keeps long pipelines readable.

Seeing what is inside an object

Since everything is an object, you can ask what properties it has. The Get-Member cmdlet shows them.

Get-Process | Get-Member

This lists the properties and methods available on a process object. It is the closest thing to reading a manual page for the data itself, and it is the habit that most speeds up learning PowerShell. Another useful command is Select-Object *, which prints every property of an object so you can see their names and values.

Formatting comes last

A common surprise is that Format-Table and Format-List should be the final step in a pipeline. They turn objects into display text, and after that the objects are gone. If you format first and try to filter afterwards, you will get odd results.

Get-Service | Where-Object Status -eq "Running" | Format-Table Name, Status

Do your filtering, sorting and selecting first, and format only at the end for display. If you need to save data, use Export-Csv or ConvertTo-Json instead, since those keep the structure.

Working with text when you must

PowerShell can still handle plain text. Get-Content reads a file line by line, and Select-String works like grep with regular expressions. External programs run too, and their output arrives as text lines that you can pipe further. The mixed approach is common: use objects for system data, and fall back to text for the output of old command line tools.

Differences worth remembering

  1. Variables start with a dollar sign everywhere, not only when reading them.
  2. Comparison uses words such as -eq, -ne, -gt and -like, not symbols such as ==.
  3. Errors are objects too, and you can handle them with try and catch.
  4. Quoting rules differ: double quotes expand variables, and single quotes do not, as in Bash.
  5. A pipeline processes objects one at a time as they arrive, so it can stream large sets without holding them all in memory.

Frequently Asked Questions

Is PowerShell only for Windows?

No. PowerShell 7 and later runs on Windows, Linux and macOS. Some cmdlets, especially those that manage Windows specific features, are only available on Windows, but the core language and pipeline work everywhere.

Why are PowerShell commands so long?

The long names are meant to be readable and consistent. Tab completion helps with typing, and many cmdlets have short aliases for interactive use. In saved scripts, full names are usually preferred because they are clearer.

Can I use Bash and PowerShell together?

Yes. You can call Bash from PowerShell, or PowerShell from Bash, as long as both are installed. Each passes its output as text across the boundary, so structure is lost unless you convert it to a format like JSON first.

Related reading