Skip to content

Commit c243621

Browse files
Update ForEach-Object -Parallel example 17
In PS v7.2, ForEach-Object -Parallel was updated to support variable access in nested script block scenarios. Example 17 in the docs currently reflects the erroneous behavior exhibited in v7.1 and prior. This change updates the example to reflect the fixed behavior in v7.2. See PS issue #11817 for more information.
1 parent 3f3cbea commit c243621

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

reference/7.4/Microsoft.PowerShell.Core/ForEach-Object.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ Output: 5
455455
> [PipelineVariable](About/about_CommonParameters.md) common parameter variables are _not_
456456
> supported in `ForEach-Object -Parallel` scenarios even with the `Using:` scope modifier.
457457
458-
### Example 17: Passing variables in nested parallel script ScriptBlockSet
458+
### Example 17: Passing variables in nested parallel scriptblocks
459459

460460
You can create a variable outside a `ForEach-Object -Parallel` scoped scriptblock and use
461461
it inside the scriptblock with the `Using:` scope modifier.
@@ -472,9 +472,10 @@ TestA
472472
TestA
473473
```
474474

475+
Beginning in PowerShell 7.2, you can create a variable inside a `ForEach-Object -Parallel` scoped
476+
scriptblock and use it inside a nested scriptblock.
477+
475478
```powershell
476-
# You CANNOT create a variable inside a scoped scriptblock
477-
# to be used in a nested foreach parallel scriptblock.
478479
$test1 = 'TestA'
479480
1..2 | ForEach-Object -Parallel {
480481
$Using:test1
@@ -486,14 +487,17 @@ $test1 = 'TestA'
486487
```
487488

488489
```Output
489-
Line |
490-
2 | 1..2 | ForEach-Object -Parallel {
491-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
492-
| The value of the using variable '$Using:test2' can't be retrieved because it has
493-
| not been set in the local session.
490+
TestA
491+
TestA
492+
TestB
493+
TestB
494+
TestB
495+
TestB
494496
```
495497

496-
The nested scriptblock can't access the `$test2` variable and an error is thrown.
498+
> [!NOTE]
499+
> In versions prior to PowerShell 7.2, the nested scriptblock can't access the `$test2` variable and
500+
> an error is thrown.
497501
498502
### Example 18: Creating multiple jobs that run scripts in parallel
499503

reference/7.5/Microsoft.PowerShell.Core/ForEach-Object.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ Output: 5
455455
> [PipelineVariable](About/about_CommonParameters.md) common parameter variables are _not_
456456
> supported in `ForEach-Object -Parallel` scenarios even with the `Using:` scope modifier.
457457
458-
### Example 17: Passing variables in nested parallel script ScriptBlockSet
458+
### Example 17: Passing variables in nested parallel scriptblocks
459459

460460
You can create a variable outside a `ForEach-Object -Parallel` scoped scriptblock and use
461461
it inside the scriptblock with the `Using:` scope modifier.
@@ -472,9 +472,10 @@ TestA
472472
TestA
473473
```
474474

475+
Beginning in PowerShell 7.2, you can create a variable inside a `ForEach-Object -Parallel` scoped
476+
scriptblock and use it inside a nested scriptblock.
477+
475478
```powershell
476-
# You CANNOT create a variable inside a scoped scriptblock
477-
# to be used in a nested foreach parallel scriptblock.
478479
$test1 = 'TestA'
479480
1..2 | ForEach-Object -Parallel {
480481
$Using:test1
@@ -486,14 +487,17 @@ $test1 = 'TestA'
486487
```
487488

488489
```Output
489-
Line |
490-
2 | 1..2 | ForEach-Object -Parallel {
491-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
492-
| The value of the using variable '$Using:test2' can't be retrieved because it has
493-
| not been set in the local session.
490+
TestA
491+
TestA
492+
TestB
493+
TestB
494+
TestB
495+
TestB
494496
```
495497

496-
The nested scriptblock can't access the `$test2` variable and an error is thrown.
498+
> [!NOTE]
499+
> In versions prior to PowerShell 7.2, the nested scriptblock can't access the `$test2` variable and
500+
> an error is thrown.
497501
498502
### Example 18: Creating multiple jobs that run scripts in parallel
499503

reference/7.6/Microsoft.PowerShell.Core/ForEach-Object.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ Output: 5
455455
> [PipelineVariable](About/about_CommonParameters.md) common parameter variables are _not_
456456
> supported in `ForEach-Object -Parallel` scenarios even with the `Using:` scope modifier.
457457
458-
### Example 17: Passing variables in nested parallel script ScriptBlockSet
458+
### Example 17: Passing variables in nested parallel scriptblocks
459459

460460
You can create a variable outside a `ForEach-Object -Parallel` scoped scriptblock and use
461461
it inside the scriptblock with the `Using:` scope modifier.
@@ -472,9 +472,10 @@ TestA
472472
TestA
473473
```
474474

475+
Beginning in PowerShell 7.2, you can create a variable inside a `ForEach-Object -Parallel` scoped
476+
scriptblock and use it inside a nested scriptblock.
477+
475478
```powershell
476-
# You CANNOT create a variable inside a scoped scriptblock
477-
# to be used in a nested foreach parallel scriptblock.
478479
$test1 = 'TestA'
479480
1..2 | ForEach-Object -Parallel {
480481
$Using:test1
@@ -486,14 +487,17 @@ $test1 = 'TestA'
486487
```
487488

488489
```Output
489-
Line |
490-
2 | 1..2 | ForEach-Object -Parallel {
491-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
492-
| The value of the using variable '$Using:test2' can't be retrieved because it has
493-
| not been set in the local session.
490+
TestA
491+
TestA
492+
TestB
493+
TestB
494+
TestB
495+
TestB
494496
```
495497

496-
The nested scriptblock can't access the `$test2` variable and an error is thrown.
498+
> [!NOTE]
499+
> In versions prior to PowerShell 7.2, the nested scriptblock can't access the `$test2` variable and
500+
> an error is thrown.
497501
498502
### Example 18: Creating multiple jobs that run scripts in parallel
499503

0 commit comments

Comments
 (0)