| external help file | PSParallelPipeline-help.xml |
|---|---|
| Module Name | PSParallelPipeline |
| online version | https://github.com/santisq/PSParallelPipeline |
| schema | 2.0.0 |
Executes parallel processing of pipeline input objects using multithreading.
Invoke-Parallel
[-ScriptBlock] <ScriptBlock>
[-InputObject <Object>]
[-ThrottleLimit <Int32>]
[-TimeoutSeconds <Int32>]
[-Variables <Hashtable>]
[-Functions <String[]>]
[-ModuleNames <String[]>]
[-ModulePaths <String[]>]
[-UseNewRunspace]
[<CommonParameters>]The Invoke-Parallel cmdlet enables parallel processing of input objects in PowerShell, including
Windows PowerShell 5.1 and PowerShell 7+, offering functionality similar to ForEach-Object -Parallel introduced in
PowerShell 7.0. It processes pipeline input across multiple threads, improving performance for tasks that benefit from
parallel execution.
$message = 'Hello world from '
0..10 | Invoke-Parallel {
$using:message + [runspace]::DefaultRunspace.InstanceId
Start-Sleep 3
}This example demonstrates parallel execution of a script block with a 3-second delay, appending a unique runspace ID to
a message. The $using: scope modifier
is used to pass the local variable $message into the parallel scope, a supported method for accessing external
variables in Invoke-Parallel.
$message = 'Hello world from '
0..10 | Invoke-Parallel {
$message + [runspace]::DefaultRunspace.InstanceId
Start-Sleep 3
} -Variables @{ message = $message }This example demonstrates the -Variables parameter, which passes the local variable $message into
the parallel scope using a hashtable. The key message in the hashtable defines the variable name available within the
script block, serving as an alternative to the $using: scope modifier.
$dict = [System.Collections.Concurrent.ConcurrentDictionary[int, object]]::new()
Get-Process | Invoke-Parallel { ($using:dict)[$_.Id] = $_ }
$dict[$PID]This example uses a thread-safe dictionary to store process objects by ID, leveraging the $using: modifier for
variable access.
$dict = [System.Collections.Concurrent.ConcurrentDictionary[int, object]]::new()
Get-Process | Invoke-Parallel { $dict[$_.Id] = $_ } -Variables @{ dict = $dict }
$dict[$PID]Similar to Example 3, this demonstrates the same functionality using -Variables instead of $using:.
function Greet { param($s) "$s hey there!" }
0..10 | Invoke-Parallel { Greet $_ } -Functions GreetThis example imports a local function Greet into the parallel scope using -Functions parameter,
allowing its use within the script block.
0..10 | Invoke-Parallel { Start-Sleep 1 } -TimeoutSeconds 3This example limits execution to 3 seconds, stopping all running script blocks and ignoring unprocessed input once the timeout is reached.
0..3 | Invoke-Parallel { [runspace]::DefaultRunspace.InstanceId } -ThrottleLimit 2
# Guid
# ----
# c945ae1f-4e66-4312-b23c-f3994965308e
# 1c6af45c-8727-4488-937a-4dfc1d259e9e
# c945ae1f-4e66-4312-b23c-f3994965308e
# 1c6af45c-8727-4488-937a-4dfc1d259e9e
0..3 | Invoke-Parallel { [runspace]::DefaultRunspace.InstanceId } -ThrottleLimit 2 -UseNewRunspace
# Guid
# ----
# 7a1c3871-6ce2-4b7f-ae90-fb1e92cd9678
# 2488be9e-15fe-4be2-882d-7d98b068c913
# d3dd7b5d-e7e3-457f-b6fb-def35fe837d7
# 9af7c222-061d-4c89-b073-375ee925e538This example contrasts default runspace reuse with the -UseNewRunspace switch, showing unique runspace IDs for each
invocation in the latter case.
Import-Csv users.csv | Invoke-Parallel { Get-ADUser $_.UserPrincipalName } -ModuleNames ActiveDirectoryThis example imports the ActiveDirectory module into the parallel scope using -ModuleNames, enabling the
Get-ADUser cmdlet within the script block.
$moduleDir = Join-Path $PSScriptRoot "CustomModule"
0..10 | Invoke-Parallel { Get-CustomCmdlet } -ModulePaths $moduleDirThis example imports a custom module from the specified directory using -ModulePaths, allowing the Get-CustomCmdlet
function to be used in the parallel script block.
Note
The path must point to a directory containing a valid PowerShell module.
Specifies an array of function names from the local session to include in the runspaces’ Initial Session State. This enables their use within the parallel script block.
Tip
This parameter is the recommended way to make local functions available in the parallel scope.
Alternatively, you can retrieve the function definition as a string (e.g., $def = ${function:Greet}.ToString()) and
use $using: to pass it into the script block, defining it there (e.g., ${function:Greet} = $using:def).
Type: String[]
Parameter Sets: (All)
Aliases: funcs
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseSpecifies the objects to process in the script block. This parameter accepts pipeline input.
Type: Object
Parameter Sets: (All)
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: FalseDefines the script block executed for each input object in parallel.
Type: ScriptBlock
Parameter Sets: (All)
Aliases:
Required: True
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseSets the maximum number of script blocks executed in parallel across multiple threads. Additional input objects wait
until the number of running script blocks falls below this limit. The default value is 5.
Type: Int32
Parameter Sets: (All)
Aliases: tl
Required: False
Position: Named
Default value: 5
Accept pipeline input: False
Accept wildcard characters: FalseSpecifies the maximum time (in seconds) to process all input objects. When the timeout is reached, running script blocks are terminated, and remaining input is discarded.
Note
A value of 0 (default) disables the timeout, allowing processing to continue until completion.
Type: Int32
Parameter Sets: (All)
Aliases: to
Required: False
Position: Named
Default value: 0
Accept pipeline input: False
Accept wildcard characters: FalseUses a new runspace for each parallel invocation instead of reusing existing runspaces in the runspace pool.
Type: SwitchParameter
Parameter Sets: (All)
Aliases: unr
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: FalseProvides a hashtable of variables to make available in the parallel scope. Keys define the variable names within the script block.
Tip
Use this parameter as an alternative to the
$using: scope modifier.
Type: Hashtable
Parameter Sets: (All)
Aliases: vars
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseSpecifies an array of module names to import into the runspaces’ Initial Session State. This allows the script block to use cmdlets and functions from the specified modules.
Tip
Use this parameter to ensure required modules are available in the parallel scope. Module names must be discoverable
via the $env:PSModulePath
environment variable, which lists installed module locations.
Type: String[]
Parameter Sets: (All)
Aliases: mn
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseSpecifies an array of file paths to directories containing PowerShell modules (e.g., .psm1 or .psd1 files) to import
into the runspaces’ Initial Session State.
This enables the script block to use cmdlets and functions from custom or local modules.
Note
Paths must be absolute or relative to the current working directory and must point to valid directories containing PowerShell modules. If an invalid path (e.g., a file or non-existent directory) is provided, a terminating error is thrown.
Type: String[]
Parameter Sets: (All)
Aliases: mp
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseThis cmdlet supports the common parameters. For more information, see about_CommonParameters.
You can pipe any object to this cmdlet.
Returns objects produced by the script block.
Invoke-Paralleluses multithreading, which may introduce overhead. For small datasets, sequential processing might be faster.- Ensure variables or collections passed to the parallel scope are thread-safe (e.g., use
ConcurrentDictionary<TKey, TValue>or similar), as shown in Example #3 and Example #4, to avoid race conditions. - By default, runspaces are reused from a pool to optimize resource usage. Using
-UseNewRunspaceincreases memory and startup time but ensures isolation.