0

If i run a script that processes a path recursively using parallel processing to execute a command on the whole tree if items, am I guaranteed that the commands I use will be executed on all items in a single directory before stepping to the next one?

Get-ChildItem $Path -Recurse -Filter *.some_file_extension | ForEach-Object -Parallel {
    my_command $_ --with_options
}
1
  • You really want to read up on the meaning of parallel processing in general (since it is not anything unique to PowerShell - which only got it in later versions), to set your expectations. 'PowerShell parallel processing'.
    – postanote
    Commented Jun 12, 2021 at 21:12

1 Answer 1

2

As per the MS Docs on regarding the feature and use case:

https://devblogs.microsoft.com/powershell/powershell-foreach-object-parallel-feature

Normally, when you use the ForEach-Object cmdlet, each object piped to the cmdlet is processed sequentially.

1..5 | 
ForEach-Object { "Hello $_"; sleep 1 }

Hello 1
Hello 2
Hello 3
Hello 4
Hello 5

(Measure-Command {
    1..5 | ForEach-Object { "Hello $_"; sleep 1 } 
}).Seconds
5

But with the new ForEach-Object -Parallel parameter set, you can run all script in parallel for each piped input object.

1..5 | 
ForEach-Object -Parallel { "Hello $_"; sleep 1; } -ThrottleLimit 5
 
Hello 1 
Hello 3 
Hello 2 
Hello 4 
Hello 5 

(Measure-Command {
    1..5 | 
ForEach-Object -Parallel { "Hello $_"; sleep 1; } -ThrottleLimit 5 
}).Seconds
1

Because each script block in the ForEach-Object example above takes 1 second to run, running all five in parallel takes only one second instead of 5 seconds when run sequentially.

Since the script blocks are run in parallel for each of the 1-5 piped input integers, the order of execution is not guaranteed. The -ThrottleLimit parameter limits the number of script blocks running in parallel at a given time, and its default value is 5.

2
  • I understand that the files are processed in parallel aka "at the same time (as far as the buffer can take items"). I know I could iterate over the folders sequentially and than feed the items to a loop marked as parallel. I'm still not 100% sure that the files would be processed in order of appearance in the directory tree (even if that means that would be a slight disconnect in which file is processing and which has finished processing). But you're right parallel processing is just that, I can just iterate sequentially over the folders if I'm still in doubt...
    – maja
    Commented Jun 13, 2021 at 7:08
  • 1
    Or look at parallel processing in combination with workflows. Though workflow has been, well, problematic. Meaning, never really worked as intended for those who did try.
    – postanote
    Commented Jun 13, 2021 at 7:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .