Skip to content

Commit d0ea7c0

Browse files
committed
updates doc
1 parent 357152c commit d0ea7c0

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

docs/en-US/Invoke-Parallel.md

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ Invoke-Parallel
2727

2828
## DESCRIPTION
2929

30-
`Invoke-Parallel` is a PowerShell cmdlet that allows parallel processing of input objects with similar capabilities as
31-
[`ForEach-Object -Parallel`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object) introduced in PowerShell v7.0.
30+
The `Invoke-Parallel` cmdlet enables parallel processing of input objects in PowerShell, 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.
3231

3332
## EXAMPLES
3433

35-
### Example 1: Run slow script in parallel batches
34+
### Example 1: Run a slow script in parallel batches
3635

3736
```powershell
3837
$message = 'Hello world from '
@@ -43,6 +42,8 @@ $message = 'Hello world from '
4342
}
4443
```
4544

45+
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](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.4#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`.
46+
4647
### Example 2: Demonstrates `-Variables` Parameter
4748

4849
```powershell
@@ -54,43 +55,47 @@ $message = 'Hello world from '
5455
} -Variables @{ message = $message }
5556
```
5657

57-
[`-Variables`](#-variables) specifies a hashtable with key / value pairs of variables to pass-in to the parallel scope. The hashtable keys defines the name for passed-in variables. This parameter is an alternative for the `$using:` scope modifier.
58+
This example demonstrates the [`-Variables` parameter](#-variables), 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.
5859

59-
### Example 3: Adding to a single thread safe instance with `$using:` scope modifier
60+
### Example 3: Adding to a thread-safe collection with `$using:`
6061

6162
```powershell
6263
$dict = [System.Collections.Concurrent.ConcurrentDictionary[int, object]]::new()
6364
Get-Process | Invoke-Parallel { ($using:dict)[$_.Id] = $_ }
6465
$dict[$PID]
6566
```
6667

67-
### Example 4: Adding to a single thread safe instance using `-Variables`
68+
This example uses a thread-safe dictionary to store process objects by ID, leveraging the `$using:` modifier for variable access.
69+
70+
### Example 4: Adding to a thread-safe collection with `-Variables`
6871

6972
```powershell
7073
$dict = [System.Collections.Concurrent.ConcurrentDictionary[int, object]]::new()
7174
Get-Process | Invoke-Parallel { $dict[$_.Id] = $_ } -Variables @{ dict = $dict }
7275
$dict[$PID]
7376
```
7477

75-
### Example 5: Demonstrates `-Functions` Parameter
78+
Similar to Example 3, this demonstrates the same functionality using `-Variables` instead of `$using:`.
79+
80+
### Example 5: Using the `-Functions` parameter
7681

7782
```powershell
7883
function Greet { param($s) "$s hey there!" }
7984
8085
0..10 | Invoke-Parallel { Greet $_ } -Functions Greet
8186
```
8287

83-
[`-Functions`](#-functions) adds locally defined functions to the runspaces [Initial Session State](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.initialsessionstate) allowing you to use them in the parallel scope.
88+
This example imports a local function `Greet` into the parallel scope using [`-Functions` parameter](#-functions), allowing its use within the script block.
8489

85-
### Example 6: Demonstrates `-TimeoutSeconds` Parameter
90+
### Example 6: Setting a timeout with `-TimeoutSeconds`
8691

8792
```powershell
8893
0..10 | Invoke-Parallel { Start-Sleep 1 } -TimeoutSeconds 3
8994
```
9095

91-
All parallel invocations are stopped when the timeout is reached and any remaining input objects to be processed are ignored.
96+
This example limits execution to 3 seconds, stopping all running script blocks and ignoring unprocessed input once the timeout is reached.
9297

93-
### Example 7: Demonstrates `-UseNewRunspace` Parameter
98+
### Example 7: Creating new runspaces with `-UseNewRunspace`
9499

95100
```powershell
96101
0..3 | Invoke-Parallel { [runspace]::DefaultRunspace.InstanceId } -ThrottleLimit 2
@@ -112,17 +117,17 @@ All parallel invocations are stopped when the timeout is reached and any remaini
112117
# 9af7c222-061d-4c89-b073-375ee925e538
113118
```
114119

115-
By default the runspaces are reused. With `-UseNewRunspace` a new runspace is created per input object.
120+
This example contrasts default runspace reuse with the `-UseNewRunspace` switch, showing unique runspace IDs for each invocation in the latter case.
116121

117122
## PARAMETERS
118123

119124
### -Functions
120125

121-
Specifies existing functions in the Local Session to have added to the runspaces [Initial Session State](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.initialsessionstate).
126+
Specifies an array of function names from the local session to include in the runspaces' [Initial Session State](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.initialsessionstate). This enables their use within the parallel script block.
122127

123128
> [!TIP]
124129
>
125-
> This method is the recommended way of passing-in local functions to the parallel scope. The alternative to this method is passing-in the function definition (as a string) to the parallel scope and define the function in it.
130+
> 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`).
126131
127132
```yaml
128133
Type: String[]
@@ -138,11 +143,7 @@ Accept wildcard characters: False
138143
139144
### -InputObject
140145
141-
Specifies the input objects to be processed in the ScriptBlock.
142-
143-
> [!NOTE]
144-
>
145-
> This parameter is intended to be bound from pipeline.
146+
Specifies the objects to process in the script block. This parameter accepts pipeline input.
146147
147148
```yaml
148149
Type: Object
@@ -158,8 +159,7 @@ Accept wildcard characters: False
158159
159160
### -ScriptBlock
160161
161-
Specifies the operation that is performed on each input object.
162-
This script block is run for every object in the pipeline.
162+
Defines the script block executed for each input object in parallel.
163163
164164
```yaml
165165
Type: ScriptBlock
@@ -175,12 +175,7 @@ Accept wildcard characters: False
175175
176176
### -ThrottleLimit
177177
178-
Specifies the number of script blocks that are invoked in parallel (Degree of Parallelism).
179-
Input objects are blocked until the running script block count falls below the ThrottleLimit.
180-
181-
> [!NOTE]
182-
>
183-
> `-ThrottleLimit` default value is `5`.
178+
Sets 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`.
184179

185180
```yaml
186181
Type: Int32
@@ -196,12 +191,11 @@ Accept wildcard characters: False
196191

197192
### -TimeoutSeconds
198193

199-
Specifies the number of seconds to wait for all input to be processed in parallel.
200-
After the specified timeout time, all running scripts are stopped and any remaining input objects to be processed are ignored.
194+
Specifies 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.
201195

202196
> [!NOTE]
203197
>
204-
> Default value of `0` disables the timeout and the cmdlet runs until all pipeline input is processed.
198+
> A value of `0` (default) disables the timeout, allowing processing to continue until completion.
205199

206200
```yaml
207201
Type: Int32
@@ -217,7 +211,7 @@ Accept wildcard characters: False
217211

218212
### -UseNewRunspace
219213

220-
Uses a new runspace for each parallel invocation instead of reusing them.
214+
Uses a new runspace for each parallel invocation instead of reusing existing runspaces in the runspace pool.
221215

222216
```yaml
223217
Type: SwitchParameter
@@ -233,12 +227,11 @@ Accept wildcard characters: False
233227

234228
### -Variables
235229

236-
Specifies a hashtable of variables to have available in the parallel scope.
237-
The hashtable keys defines the name for passed-in variables.
230+
Provides a hashtable of variables to make available in the parallel scope. Keys define the variable names within the script block.
238231

239232
> [!TIP]
240233
>
241-
> This parameter is an alternative for the [`$using:` scope modifier](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.4#scope-modifiers).
234+
> Use this parameter as an alternative to the [`$using:` scope modifier](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.4#scope-modifiers).
242235

243236
```yaml
244237
Type: Hashtable
@@ -258,12 +251,30 @@ This cmdlet supports the common parameters. For more information, see [about_Com
258251

259252
## INPUTS
260253

261-
### Object
254+
### System.Object
262255

263256
You can pipe any object to this cmdlet.
264257

265258
## OUTPUTS
266259

267-
### Object
260+
### System.Object
261+
262+
Returns objects produced by the script block.
263+
264+
## NOTES
265+
266+
- `Invoke-Parallel` uses multithreading, which may introduce overhead. For small datasets, sequential processing might be faster.
267+
- Ensure variables or collections passed to the parallel scope are thread-safe (e.g., `[System.Collections.Concurrent.ConcurrentDictionary]`), as shown in Examples 3 and 4.
268+
- By default, runspaces are reused from a pool to optimize resource usage. Using `-UseNewRunspace` increases memory and startup time but ensures isolation.
269+
270+
## RELATED LINKS
271+
272+
Online Version
273+
274+
[__ForEach-Object -Parallel__](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object)
275+
276+
[__Runspaces Overview__](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.runspace?view=powershellsdk-7.4.0)
277+
278+
[__Managed threading best practices__](https://learn.microsoft.com/en-us/dotnet/standard/threading/managed-threading-best-practices)
268279

269-
This cmdlet returns objects that are determined by the script block.
280+
[__Thread-safe collections__](https://learn.microsoft.com/en-us/dotnet/standard/collections/thread-safe/)

0 commit comments

Comments
 (0)