Parallel processing with Foreach-Object

I have a fairly simple request but it doesn’t seem to work

Essentially I have an xml file which has n elements.

I iterate over the elements using Foreach-Object

As I am processing each element I use Start-Process to kick off a task.

I check for the last item in the collection and when I hit it, the same Start-Process as the others, invokes but this time with a -Wait.

Once all have been completed, I would expect the -End block to run but it never does

I can post code if you wish but it is a pretty straight forward thing, i think

when you use -Wait, Start-Process waits for the process to complete. You can use -PassThru instead of wait and catch the output in an array. Once the iteration is completed use the array with Wait-Process cmdlet.

ok, let me play with that. However, I do want it to wait. I would have thought when the final enumeration completed, the one with the wait, it would have run the finish block

Here is a gist just to show the full thing.


Initial testing seems to work. But now I am really curious why the first one didn’t work. Just a teaching moment please