Working of Pipeline in Powershell

Hi guys, this is bothering me…

I was reading about how pipeline works in powershell at
[1]: about Pipelines - PowerShell | Microsoft Docs.

and got to know that pipeline delivers one object at a time.

So, this

Get-Service | Format-Table -Property Name, DependentServices

is different from this

Format-Table -InputObject (Get-Service) -Property Name, DependentServices

So here, going by the explanation, in the first case, the Format-Table works on one object at at time and in the second example, Format-Table works on an array of objects. Please correct me if I am wrong.

If this is the case, then I wonder how does Sort-Object and other cmdlets that need to work on collection of data work with pipe character.
When I do :

Get-Service | Sort-Object

How is Sort-Object able to sort if it just gets to work with one object at a time. So, assume there are 100 service objects that are to be passed to Sort-Object. Will Sort-Object be called 100 times (each for one object) and how will that yield in Sorted results that I see on the screen.

I would really appreciate if someone can clarify it?

Pragun,
Welcome to the forum.

As almost always in life there are certain exceptions from each rule. So there are this time. In some cases the pipeline waits until it has all the needed elements to do its job.

On modern computers it’s hard to see but there’s already a difference between

Get-Service | Format-Table

and

Get-Service | Format-Table -AutoSize

In the seccond case the cmdlet Fomrat-Table has to wait for ALL results before it can calculate the output because it does not know before what’s the longest line in the output.

The same happens when you use Sort-Object. PowerShell will be blocked until all reults from the last pipeline step are available. :wink:

1 Like