Skip to content

Commit 2a19cbf

Browse files
Update ForEach-Object example 17 to reflect current behavior (#11997)
* 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. * Fix schema violation --------- Co-authored-by: Sean Wheeler <[email protected]>
1 parent 3f3cbea commit 2a19cbf

File tree

3 files changed

+45
-72
lines changed

3 files changed

+45
-72
lines changed

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: System.Management.Automation.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Core
5-
ms.date: 04/26/2024
5+
ms.date: 04/13/2025
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.4&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: ForEach-Object
@@ -455,27 +455,15 @@ 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

460-
You can create a variable outside a `ForEach-Object -Parallel` scoped scriptblock and use
461-
it inside the scriptblock with the `Using:` scope modifier.
460+
You can create a variable outside a `ForEach-Object -Parallel` scoped scriptblock and use it inside
461+
the scriptblock with the `Using:` scope modifier. Beginning in PowerShell 7.2, you can create a
462+
variable inside a `ForEach-Object -Parallel` scoped scriptblock and use it inside a nested
463+
scriptblock.
462464

463465
```powershell
464466
$test1 = 'TestA'
465-
1..2 | ForEach-Object -Parallel {
466-
$Using:test1
467-
}
468-
```
469-
470-
```Output
471-
TestA
472-
TestA
473-
```
474-
475-
```powershell
476-
# You CANNOT create a variable inside a scoped scriptblock
477-
# to be used in a nested foreach parallel scriptblock.
478-
$test1 = 'TestA'
479467
1..2 | ForEach-Object -Parallel {
480468
$Using:test1
481469
$test2 = 'TestB'
@@ -486,14 +474,17 @@ $test1 = 'TestA'
486474
```
487475

488476
```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.
477+
TestA
478+
TestA
479+
TestB
480+
TestB
481+
TestB
482+
TestB
494483
```
495484

496-
The nested scriptblock can't access the `$test2` variable and an error is thrown.
485+
> [!NOTE]
486+
> In versions prior to PowerShell 7.2, the nested scriptblock can't access the `$test2` variable and
487+
> an error is thrown.
497488
498489
### Example 18: Creating multiple jobs that run scripts in parallel
499490

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: System.Management.Automation.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Core
5-
ms.date: 04/26/2024
5+
ms.date: 04/13/2025
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.5&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: ForEach-Object
@@ -455,27 +455,15 @@ 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

460-
You can create a variable outside a `ForEach-Object -Parallel` scoped scriptblock and use
461-
it inside the scriptblock with the `Using:` scope modifier.
460+
You can create a variable outside a `ForEach-Object -Parallel` scoped scriptblock and use it inside
461+
the scriptblock with the `Using:` scope modifier. Beginning in PowerShell 7.2, you can create a
462+
variable inside a `ForEach-Object -Parallel` scoped scriptblock and use it inside a nested
463+
scriptblock.
462464

463465
```powershell
464466
$test1 = 'TestA'
465-
1..2 | ForEach-Object -Parallel {
466-
$Using:test1
467-
}
468-
```
469-
470-
```Output
471-
TestA
472-
TestA
473-
```
474-
475-
```powershell
476-
# You CANNOT create a variable inside a scoped scriptblock
477-
# to be used in a nested foreach parallel scriptblock.
478-
$test1 = 'TestA'
479467
1..2 | ForEach-Object -Parallel {
480468
$Using:test1
481469
$test2 = 'TestB'
@@ -486,14 +474,17 @@ $test1 = 'TestA'
486474
```
487475

488476
```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.
477+
TestA
478+
TestA
479+
TestB
480+
TestB
481+
TestB
482+
TestB
494483
```
495484

496-
The nested scriptblock can't access the `$test2` variable and an error is thrown.
485+
> [!NOTE]
486+
> In versions prior to PowerShell 7.2, the nested scriptblock can't access the `$test2` variable and
487+
> an error is thrown.
497488
498489
### Example 18: Creating multiple jobs that run scripts in parallel
499490

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: System.Management.Automation.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Core
5-
ms.date: 04/26/2024
5+
ms.date: 04/13/2025
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.6&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: ForEach-Object
@@ -455,27 +455,15 @@ 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

460-
You can create a variable outside a `ForEach-Object -Parallel` scoped scriptblock and use
461-
it inside the scriptblock with the `Using:` scope modifier.
460+
You can create a variable outside a `ForEach-Object -Parallel` scoped scriptblock and use it inside
461+
the scriptblock with the `Using:` scope modifier. Beginning in PowerShell 7.2, you can create a
462+
variable inside a `ForEach-Object -Parallel` scoped scriptblock and use it inside a nested
463+
scriptblock.
462464

463465
```powershell
464466
$test1 = 'TestA'
465-
1..2 | ForEach-Object -Parallel {
466-
$Using:test1
467-
}
468-
```
469-
470-
```Output
471-
TestA
472-
TestA
473-
```
474-
475-
```powershell
476-
# You CANNOT create a variable inside a scoped scriptblock
477-
# to be used in a nested foreach parallel scriptblock.
478-
$test1 = 'TestA'
479467
1..2 | ForEach-Object -Parallel {
480468
$Using:test1
481469
$test2 = 'TestB'
@@ -486,14 +474,17 @@ $test1 = 'TestA'
486474
```
487475

488476
```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.
477+
TestA
478+
TestA
479+
TestB
480+
TestB
481+
TestB
482+
TestB
494483
```
495484

496-
The nested scriptblock can't access the `$test2` variable and an error is thrown.
485+
> [!NOTE]
486+
> In versions prior to PowerShell 7.2, the nested scriptblock can't access the `$test2` variable and
487+
> an error is thrown.
497488
498489
### Example 18: Creating multiple jobs that run scripts in parallel
499490

0 commit comments

Comments
 (0)