💾 Archived View for pwshnotes.flounder.online › gemlog › 2021-10-25-pipelines.gmi captured on 2021-12-03 at 14:04:38. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Pipelines

A pipe connects two cmdlets so that they can be used together.

For example, this code generates a long list of files:

Get-ChildItem -Path /usr/bin

    Directory: /usr/bin

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-r--            9/4/2021 12:26 PM                core_perl
d-r--           9/17/2021  7:10 AM                lou_maketable.d
d-r--           7/18/2021 12:20 PM                site_perl
d-r--          10/16/2021 11:57 PM                vendor_perl
--r--           9/29/2021  8:56 AM          59552 [
l----           8/31/2021  8:28 AM              8 2to3 -> /usr/bin/2to3-3.9
--r--           8/31/2021  8:28 AM             95 2to3-3.9
--r--           8/10/2021  4:11 AM          14168 411toppm
--r--          10/15/2020 12:56 AM          14176 4channels
--r--            9/7/2021 10:11 PM             36 7z
...

Whereas, the code below generates a shorter list of only files which contain the phrase "term". The shorter list is made possible by joining the Get-ChildItem and Where-Object cmdlets using a pipe.

Get-ChildItem -Path /usr/bin | Where-Object { $_.Name -match "term" }

    Directory: /usr/bin

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
--r--            2/6/2020 12:14 AM           5112 foomatic-printermap-to-gutenp
                                                  rint-xml
--r--           8/10/2021  4:11 AM          14168 ppmtoterm
--r--           8/16/2021  9:10 AM          47152 setterm
--r--            6/2/2021 10:40 PM         346528 xfce4-terminal

A pipe connects the output of one cmdlet to the input of another cmdlet.

The symbol used to represent these connections is the vertical bar or pipe symbol: | On my keyboard it is typed by pressing Shift + \

The pipe symbol is placed between two cmdlets. Then, when processed, output from the left cmdlet will be input to the right cmdlet.

While I'm sure there is some technical limit regarding the number of characters typed on one line in the shell or the number of pipe segments, practically there is no limit to the number of cmdlets which can be joined by pipes. It is common to see between two and ten cmdlets joined by pipes. Pipelines can be split across typewritten lines as long as the interpreter sees pipe symbols at the end of each line.

Get-ChildItem -Path /usr/bin | 
  Where-Object { $_.PSIsContainer -eq $false } | 
  Select-Object -First 1 | 
  Get-Member | 
  Select-Object -Property TypeName -Unique

TypeName
--------
System.IO.FileInfo

Conceptually, the flow of data from one cmdlet to the next through pipes and output to a final destination is known as a pipeline. The typewritten arrangement of cmdlets and pipe symbols might also be referred to as a pipeline.

Pipelines are important in PowerShell because they allow the narrowly focused abilities of individual cmdlets to be joined into arbitrarily complex combinations which can produce any change or output.

Pipelines are also important because, along with objects, they are one of the two fundamental building blocks of PowerShell literacy. Most administrators intuitively understand the role of cmdlets or commands. But PowerShell introduces objects and it heavily depends on pipelines to function. Understanding objects and pipelines will allow users to read and interpret the vast majority of PowerShell code.

Unlike objects, pipelines are not exclusive to PowerShell. And there is a long history of pipelines in command-line shells. Because PowerShell has access to the underlying native commands available on a system, it is possible to create pipelines with native commands or to mix native commands and cmdlets in one pipeline. There can be a role for this if you are willing to parse the text-based output of native commands. However, pipelines in PowerShell work best when cmdlets are used exclusively and the data flowing between segments is object-based: the typical output of cmdlets.

Pipelines are common in PowerShell. Many code examples will depend on pipelines to work. And pipelines allow complex processing by combining simpler fragments of code. It is important to be able to recognize pipelines and to understand their functioning.

Created: Monday, October 25, 2021

Updated: Monday, October 25, 2021