You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en-US/Invoke-Parallel.md
+48-37Lines changed: 48 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -27,12 +27,11 @@ Invoke-Parallel
27
27
28
28
## DESCRIPTION
29
29
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.
32
31
33
32
## EXAMPLES
34
33
35
-
### Example 1: Run slow script in parallel batches
34
+
### Example 1: Run a slow script in parallel batches
36
35
37
36
```powershell
38
37
$message = 'Hello world from '
@@ -43,6 +42,8 @@ $message = 'Hello world from '
43
42
}
44
43
```
45
44
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
+
46
47
### Example 2: Demonstrates `-Variables` Parameter
47
48
48
49
```powershell
@@ -54,43 +55,47 @@ $message = 'Hello world from '
54
55
} -Variables @{ message = $message }
55
56
```
56
57
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.
58
59
59
-
### Example 3: Adding to a single threadsafe instance with `$using:` scope modifier
60
+
### Example 3: Adding to a thread-safe collection with `$using:`
[`-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.
84
89
85
-
### Example 6: Demonstrates `-TimeoutSeconds` Parameter
90
+
### Example 6: Setting a timeout with `-TimeoutSeconds`
@@ -112,17 +117,17 @@ All parallel invocations are stopped when the timeout is reached and any remaini
112
117
# 9af7c222-061d-4c89-b073-375ee925e538
113
118
```
114
119
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.
116
121
117
122
## PARAMETERS
118
123
119
124
### -Functions
120
125
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.
122
127
123
128
> [!TIP]
124
129
>
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`).
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`.
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.
201
195
202
196
> [!NOTE]
203
197
>
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.
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.
238
231
239
232
> [!TIP]
240
233
>
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).
242
235
243
236
```yaml
244
237
Type: Hashtable
@@ -258,12 +251,30 @@ This cmdlet supports the common parameters. For more information, see [about_Com
258
251
259
252
## INPUTS
260
253
261
-
### Object
254
+
### System.Object
262
255
263
256
You can pipe any object to this cmdlet.
264
257
265
258
## OUTPUTS
266
259
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.
0 commit comments