This script demonstrates the use of parallel processing in PowerShell 7 with ForEach-Object -Parallel, comparing its performance to sequential processing in PowerShell 5. By utilizing parallel processing, execution time can be significantly reduced, maximizing hardware resource usage on multicore systems.
The script iterates over a list ($dataArray) of numbers from 1 to 100, simulating a 1-second operation for each item. It measures the execution time in two configurations:
- PowerShell 7: Uses
ForEach-Object -Parallelwith a concurrency limit (-ThrottleLimit) of 64. - PowerShell 5: Processes the items sequentially, without parallelism.
- PowerShell 7 or PowerShell 5 is required to run the script.
- PowerShell 7 is recommended to explore the benefits of parallel processing.
- Save the code in a
.ps1file, for example,ParallelProcessingExample.ps1. - Open PowerShell.
- Navigate to the directory where the
.ps1file is saved. - Run the script with the following command:
.\ParallelProcessingExample.ps1