Skip to content

Commit 49e015e

Browse files
committed
Add note about Property wrapping in ForEach() method
1 parent e8b7424 commit 49e015e

4 files changed

Lines changed: 169 additions & 7 deletions

File tree

reference/5.1/Microsoft.PowerShell.Core/About/about_Arrays.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes arrays, which are data structures designed to store collections of items.
33
Locale: en-US
4-
ms.date: 01/18/2026
4+
ms.date: 03/24/2026
55
no-loc: [Count, Length, LongLength, Rank, ForEach, Clear, Default, First, Last, SkipUntil, Until, Split, Tuple]
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
@@ -583,6 +583,47 @@ every item in the collection.
583583
Wednesday, June 20, 2018 9:21:57 AM
584584
```
585585

586+
> [!NOTE]
587+
> The `ForEach()` method wraps properties into a collection before enumeration.
588+
> This wrapping means that accessing the first element of the original
589+
> collection requires two indices `[0][0]`.
590+
591+
Consider the following example:
592+
593+
```powershell
594+
$c = [pscustomobject]@{
595+
Value = @(0..10)
596+
}
597+
$d = [pscustomobject]@{
598+
Value = @(11..21)
599+
}
600+
```
601+
602+
As you can see, both `$c` and `$d` have a property named **Value** that
603+
contains an array of 11 integers. When you use the `ForEach()` method to access
604+
the **Value** property of both objects, the result is a collection of two
605+
arrays. To access the first element of the first array, you need to use two
606+
indices.
607+
608+
```powershell
609+
PS> ($c, $d).ForEach('Value').Count # 2-element collection
610+
2
611+
PS> ($c, $d).ForEach('Value')[0].Count # Each is an array with 11 elements
612+
11
613+
PS> ($c, $d).ForEach('Value')[0][0] # First element of the first array
614+
0
615+
PS> ($c, $d).ForEach('Value')[1][0] # First element of the second array
616+
11
617+
```
618+
619+
This is different that using the `ForEach()` method using a scriptblock to
620+
access the **Value** property of each object.
621+
622+
```powershell
623+
PS> ($c, $d).ForEach({$_.Value}).Count # 22-element collection
624+
22
625+
```
626+
586627
#### ForEach(string methodName)
587628

588629
#### ForEach(string methodName, object[] arguments)

reference/7.4/Microsoft.PowerShell.Core/About/about_Arrays.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
description: Describes arrays, which are data structures designed to store collections of items.
33
Locale: en-US
4-
ms.date: 01/18/2026
4+
ms.date: 03/24/2026
55
no-loc: [Count, Length, LongLength, Rank, ForEach, Clear, Default, First, Last, SkipUntil, Until, Split, Tuple]
6-
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-5.1&WT.mc_id=ps-gethelp
6+
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7.4&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: about_Arrays
99
---
@@ -583,6 +583,47 @@ every item in the collection.
583583
Wednesday, June 20, 2018 9:21:57 AM
584584
```
585585

586+
> [!NOTE]
587+
> The `ForEach()` method wraps properties into a collection before enumeration.
588+
> This wrapping means that accessing the first element of the original
589+
> collection requires two indices `[0][0]`.
590+
591+
Consider the following example:
592+
593+
```powershell
594+
$c = [pscustomobject]@{
595+
Value = @(0..10)
596+
}
597+
$d = [pscustomobject]@{
598+
Value = @(11..21)
599+
}
600+
```
601+
602+
As you can see, both `$c` and `$d` have a property named **Value** that
603+
contains an array of 11 integers. When you use the `ForEach()` method to access
604+
the **Value** property of both objects, the result is a collection of two
605+
arrays. To access the first element of the first array, you need to use two
606+
indices.
607+
608+
```powershell
609+
PS> ($c, $d).ForEach('Value').Count # 2-element collection
610+
2
611+
PS> ($c, $d).ForEach('Value')[0].Count # Each is an array with 11 elements
612+
11
613+
PS> ($c, $d).ForEach('Value')[0][0] # First element of the first array
614+
0
615+
PS> ($c, $d).ForEach('Value')[1][0] # First element of the second array
616+
11
617+
```
618+
619+
This is different that using the `ForEach()` method using a scriptblock to
620+
access the **Value** property of each object.
621+
622+
```powershell
623+
PS> ($c, $d).ForEach({$_.Value}).Count # 22-element collection
624+
22
625+
```
626+
586627
#### ForEach(string methodName)
587628

588629
#### ForEach(string methodName, object[] arguments)
@@ -1124,5 +1165,3 @@ LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;}
11241165
[13]: about_While.md
11251166
[14]: https://wikipedia.org/wiki/Row-_and_column-major_order
11261167

1127-
1128-

reference/7.5/Microsoft.PowerShell.Core/About/about_Arrays.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes arrays, which are data structures designed to store collections of items.
33
Locale: en-US
4-
ms.date: 01/18/2026
4+
ms.date: 03/24/2026
55
no-loc: [Count, Length, LongLength, Rank, ForEach, Clear, Default, First, Last, SkipUntil, Until, Split, Tuple]
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7.5&WT.mc_id=ps-gethelp
77
schema: 2.0.0
@@ -583,6 +583,47 @@ every item in the collection.
583583
Wednesday, June 20, 2018 9:21:57 AM
584584
```
585585

586+
> [!NOTE]
587+
> The `ForEach()` method wraps properties into a collection before enumeration.
588+
> This wrapping means that accessing the first element of the original
589+
> collection requires two indices `[0][0]`.
590+
591+
Consider the following example:
592+
593+
```powershell
594+
$c = [pscustomobject]@{
595+
Value = @(0..10)
596+
}
597+
$d = [pscustomobject]@{
598+
Value = @(11..21)
599+
}
600+
```
601+
602+
As you can see, both `$c` and `$d` have a property named **Value** that
603+
contains an array of 11 integers. When you use the `ForEach()` method to access
604+
the **Value** property of both objects, the result is a collection of two
605+
arrays. To access the first element of the first array, you need to use two
606+
indices.
607+
608+
```powershell
609+
PS> ($c, $d).ForEach('Value').Count # 2-element collection
610+
2
611+
PS> ($c, $d).ForEach('Value')[0].Count # Each is an array with 11 elements
612+
11
613+
PS> ($c, $d).ForEach('Value')[0][0] # First element of the first array
614+
0
615+
PS> ($c, $d).ForEach('Value')[1][0] # First element of the second array
616+
11
617+
```
618+
619+
This is different that using the `ForEach()` method using a scriptblock to
620+
access the **Value** property of each object.
621+
622+
```powershell
623+
PS> ($c, $d).ForEach({$_.Value}).Count # 22-element collection
624+
22
625+
```
626+
586627
#### ForEach(string methodName)
587628

588629
#### ForEach(string methodName, object[] arguments)

reference/7.6/Microsoft.PowerShell.Core/About/about_Arrays.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes arrays, which are data structures designed to store collections of items.
33
Locale: en-US
4-
ms.date: 01/18/2026
4+
ms.date: 03/24/2026
55
no-loc: [Count, Length, LongLength, Rank, ForEach, Clear, Default, First, Last, SkipUntil, Until, Split, Tuple]
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7.6&WT.mc_id=ps-gethelp
77
schema: 2.0.0
@@ -590,6 +590,47 @@ every item in the collection.
590590
Wednesday, June 20, 2018 9:21:57 AM
591591
```
592592

593+
> [!NOTE]
594+
> The `ForEach()` method wraps properties into a collection before enumeration.
595+
> This wrapping means that accessing the first element of the original
596+
> collection requires two indices `[0][0]`.
597+
598+
Consider the following example:
599+
600+
```powershell
601+
$c = [pscustomobject]@{
602+
Value = @(0..10)
603+
}
604+
$d = [pscustomobject]@{
605+
Value = @(11..21)
606+
}
607+
```
608+
609+
As you can see, both `$c` and `$d` have a property named **Value** that
610+
contains an array of 11 integers. When you use the `ForEach()` method to access
611+
the **Value** property of both objects, the result is a collection of two
612+
arrays. To access the first element of the first array, you need to use two
613+
indices.
614+
615+
```powershell
616+
PS> ($c, $d).ForEach('Value').Count # 2-element collection
617+
2
618+
PS> ($c, $d).ForEach('Value')[0].Count # Each is an array with 11 elements
619+
11
620+
PS> ($c, $d).ForEach('Value')[0][0] # First element of the first array
621+
0
622+
PS> ($c, $d).ForEach('Value')[1][0] # First element of the second array
623+
11
624+
```
625+
626+
This is different that using the `ForEach()` method using a scriptblock to
627+
access the **Value** property of each object.
628+
629+
```powershell
630+
PS> ($c, $d).ForEach({$_.Value}).Count # 22-element collection
631+
22
632+
```
633+
593634
#### ForEach(string methodName)
594635

595636
#### ForEach(string methodName, object[] arguments)

0 commit comments

Comments
 (0)