count pipeline objects within Begin{}Process{}End{} blocks


I have a use case, that I must use Begin{}Process{}End{} blocks for a function.

And I pipe the objects to this function.

Now I need to count the pipeline objects in the function, i’m told that, I can iterate all the objects in Process{} block and count the number, and do my real taks in End{} block. This methode works, but not beautiful, this method go throug every object at first.

Im also told that not use Begin{}Process{}End{} blocks, that I will have $Input.count, but I must use Begin{}Process{}End{} blocks :slight_smile:

Can anyone help me please ?


You can’t know in advance. Objects are piped in one at a time, so you can’t know in the BEGIN block how many there will be.

What you can do is simply increment a variable in the PROCESS block, and save each incoming object into an array. Then put the rest of your code into the END block. You’ll know how many objects you have then, and you can do whatever you need. This will, however, block the pipeline - like Sort-Object does - and will consume more memory, as you’re caching objects rather than dealing with them one at a time.

Hi Jones,

Thx for you reply.
You suggestion is what I knew, doing the count in Process{}, and doing the real task in End{}.

It will be appreciated that Microsoft can directly implement an variable of the pipeline objects count for Begin{}Process{}End{}.

With this variable, for example my Wirte-Progress will be more beautiful because it will have the percentage information :slight_smile:

Your kind request of Microsoft reflects a fundamental misunderstanding of how the pipeline works. As Don mentioned, the pipeline processes one object at a time as they arrive. At any given point in the pipeline, it has NO concept how much is still to come – it could be easily affected by varying input or filtering.

So as much as you would like a pretty progress bar, it’s not going to happen this way.

Thx Bob !
So I won’t display the percentage in write-progress.
No choice.

If your intention is to show a process bar and use Write-Progress. You can keep a $TotalCount as well as a $ProcessedCount. You can use Write-Progress to show the growing number of items to be processed, then once you get to End{} The progress will show the percentage of the task “Completed”

Depending upon what you’re doing, and if your Process{} is filtering anything or doing any work other than counting. You may be able to show the percentage of completed vs. total piped in. This will be a moving target as the total will grow while the completed does not. It may take some getting used to seeing the percentage shrink as it goes on, but if you display the “completed / total” in the message it maye become clear.
3 or 40 Completed…
3 of 56 Completed…
7 of 57 Completed…
24 of 83 Completed…
83 of 83 Comepleted… Done