header image

PowerShell Pipeline

Posted by: | December 18, 2019 Comments Off on PowerShell Pipeline |

With PowerShell v7 getting close to being finished I thought it might be time to look at some of the longstanding PowerShell features that may get taken for granted with the concentration on newer features. Starting point is the PowerShell Pipeline.

 

The pipeline is one of the fundamental features of PowerShell. In some respects its strongest feature. The ability to link commands together into a pipeline is not new, or unique, to PowerShell but what is unique is what’s passed along the pipeline. Traditional shells pass text output between commands – PowerShell passes .NET objects.

 

Well, to be 100% accurate PowerShell passes .NET objects that have a wrapper. The wrapper may, or may not, modify the object. Properties, and methods, can added or hidden.

 

Irrespective of its actual form – and for many purposes you can actually ignore the modifications PowerShell makes – you’re still working with .NET objects.

 

One of the design goals of PowerShell was to make it composable. Small pieces of code (cmdlets) are linked together (the pipeline) to supply the answer to a problem. For instance:

PS> Get-Process | Sort-Object CPU -Descending | Select-Object -First 5

 

You fetch the processes, sort by their CPU usage (highest first) and select the top 5 CPU users.

Three simple commands linked in a pipeline give a much more sophisticated outcome.

 

I can remember writing VBScript code to discover which users in a 12,000 seat Active Directory had Outlook Web Access (OWA) enabled. It took 78 lines of code. When the Exchange cmdlets became available it was a single pipeline.

 

Much has been made recently of the fact that the pipeline is slow. It may not be the fastest way to get something done but a PowerShell pipeline is easy to understand and maintain than some of the alternatives that are coming into the language.

 

The PowerShell pipeline may be one of the oldest features in PowerShell but its still one of the strongest and most defining features. If that changes then its probably time to look for an alternative.

under: PowerShell

Comments are closed.

Categories