Skip to content

Commit ea32d33

Browse files
authored
Fixes #12879 - Add note about Property wrapping in ForEach() method (#12882)
* Add note about Property wrapping in ForEach() method * Fix typos and expand description of behavior
1 parent e8b7424 commit ea32d33

File tree

4 files changed

+233
-15
lines changed

4 files changed

+233
-15
lines changed

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

Lines changed: 58 additions & 3 deletions
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,12 +583,67 @@ 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+
> Using `ForEach()` normally returns all items in both array. However, if you
589+
> want to access elements of the wrapped collection, you need to use two
590+
> indices.
591+
592+
Consider the following example where the object `$myObject` has a property with
593+
single value and a property containing an array of 11 integers.
594+
595+
```powershell
596+
$myObject = [pscustomobject]@{
597+
singleValue = 'Hello'
598+
arrayValue = @(0..10)
599+
}
600+
```
601+
602+
When you use the `ForEach()` method to access a property of the object, the
603+
property is wrapped in a collection.
604+
605+
```powershell
606+
PS> $myObject.ForEach('singleValue').GetType().Name
607+
Collection`1
608+
PS> $myObject.ForEach('singleValue')[0].GetType().Name
609+
String
610+
PS> $myObject.ForEach('singleValue') # Enumerate the collection object
611+
Hello
612+
```
613+
614+
To access the an element of the array, you need to use two indices.
615+
616+
```powershell
617+
PS> $myObject.ForEach('arrayValue').GetType().Name
618+
Collection`1
619+
# A single Collection item
620+
PS> $myObject.ForEach('arrayValue').Count
621+
1
622+
# First item in the collection is an array of 11 items
623+
PS> $myObject.ForEach('Value')[0].Count
624+
11
625+
# Access the first item in the array of 11 items
626+
PS> $myObject.ForEach('Value')[0][0]
627+
0
628+
```
629+
630+
This is different than using the `ForEach()` method using with a scriptblock to
631+
access the **Value** property of each object.
632+
633+
```powershell
634+
PS> $myObject.ForEach({$_.Value}).Count # An array of 11 items
635+
11
636+
```
637+
638+
Use the scriptblock syntax to avoid the wrapping behavior when you want to
639+
access complex property types, such as arrays or nested objects.
640+
586641
#### ForEach(string methodName)
587642

588643
#### ForEach(string methodName, object[] arguments)
589644

590-
Lastly, `ForEach()` methods can be used to execute a method on every item in
591-
the collection.
645+
You can use the `ForEach()` method to execute an object's method on every item
646+
in the collection.
592647

593648
```powershell
594649
("one", "two", "three").ForEach("ToUpper")

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

Lines changed: 59 additions & 6 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,12 +583,67 @@ 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+
> Using `ForEach()` normally returns all items in both array. However, if you
589+
> want to access elements of the wrapped collection, you need to use two
590+
> indices.
591+
592+
Consider the following example where the object `$myObject` has a property with
593+
single value and a property containing an array of 11 integers.
594+
595+
```powershell
596+
$myObject = [pscustomobject]@{
597+
singleValue = 'Hello'
598+
arrayValue = @(0..10)
599+
}
600+
```
601+
602+
When you use the `ForEach()` method to access a property of the object, the
603+
property is wrapped in a collection.
604+
605+
```powershell
606+
PS> $myObject.ForEach('singleValue').GetType().Name
607+
Collection`1
608+
PS> $myObject.ForEach('singleValue')[0].GetType().Name
609+
String
610+
PS> $myObject.ForEach('singleValue') # Enumerate the collection object
611+
Hello
612+
```
613+
614+
To access the an element of the array, you need to use two indices.
615+
616+
```powershell
617+
PS> $myObject.ForEach('arrayValue').GetType().Name
618+
Collection`1
619+
# A single Collection item
620+
PS> $myObject.ForEach('arrayValue').Count
621+
1
622+
# First item in the collection is an array of 11 items
623+
PS> $myObject.ForEach('Value')[0].Count
624+
11
625+
# Access the first item in the array of 11 items
626+
PS> $myObject.ForEach('Value')[0][0]
627+
0
628+
```
629+
630+
This is different than using the `ForEach()` method using with a scriptblock to
631+
access the **Value** property of each object.
632+
633+
```powershell
634+
PS> $myObject.ForEach({$_.Value}).Count # An array of 11 items
635+
11
636+
```
637+
638+
Use the scriptblock syntax to avoid the wrapping behavior when you want to
639+
access complex property types, such as arrays or nested objects.
640+
586641
#### ForEach(string methodName)
587642

588643
#### ForEach(string methodName, object[] arguments)
589644

590-
Lastly, `ForEach()` methods can be used to execute a method on every item in
591-
the collection.
645+
You can use the `ForEach()` method to execute an object's method on every item
646+
in the collection.
592647

593648
```powershell
594649
("one", "two", "three").ForEach("ToUpper")
@@ -1124,5 +1179,3 @@ LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;}
11241179
[13]: about_While.md
11251180
[14]: https://wikipedia.org/wiki/Row-_and_column-major_order
11261181

1127-
1128-

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

Lines changed: 58 additions & 3 deletions
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,12 +583,67 @@ 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+
> Using `ForEach()` normally returns all items in both array. However, if you
589+
> want to access elements of the wrapped collection, you need to use two
590+
> indices.
591+
592+
Consider the following example where the object `$myObject` has a property with
593+
single value and a property containing an array of 11 integers.
594+
595+
```powershell
596+
$myObject = [pscustomobject]@{
597+
singleValue = 'Hello'
598+
arrayValue = @(0..10)
599+
}
600+
```
601+
602+
When you use the `ForEach()` method to access a property of the object, the
603+
property is wrapped in a collection.
604+
605+
```powershell
606+
PS> $myObject.ForEach('singleValue').GetType().Name
607+
Collection`1
608+
PS> $myObject.ForEach('singleValue')[0].GetType().Name
609+
String
610+
PS> $myObject.ForEach('singleValue') # Enumerate the collection object
611+
Hello
612+
```
613+
614+
To access the an element of the array, you need to use two indices.
615+
616+
```powershell
617+
PS> $myObject.ForEach('arrayValue').GetType().Name
618+
Collection`1
619+
# A single Collection item
620+
PS> $myObject.ForEach('arrayValue').Count
621+
1
622+
# First item in the collection is an array of 11 items
623+
PS> $myObject.ForEach('Value')[0].Count
624+
11
625+
# Access the first item in the array of 11 items
626+
PS> $myObject.ForEach('Value')[0][0]
627+
0
628+
```
629+
630+
This is different than using the `ForEach()` method using with a scriptblock to
631+
access the **Value** property of each object.
632+
633+
```powershell
634+
PS> $myObject.ForEach({$_.Value}).Count # An array of 11 items
635+
11
636+
```
637+
638+
Use the scriptblock syntax to avoid the wrapping behavior when you want to
639+
access complex property types, such as arrays or nested objects.
640+
586641
#### ForEach(string methodName)
587642

588643
#### ForEach(string methodName, object[] arguments)
589644

590-
Lastly, `ForEach()` methods can be used to execute a method on every item in
591-
the collection.
645+
You can use the `ForEach()` method to execute an object's method on every item
646+
in the collection.
592647

593648
```powershell
594649
("one", "two", "three").ForEach("ToUpper")

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

Lines changed: 58 additions & 3 deletions
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,12 +590,67 @@ 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+
> Using `ForEach()` normally returns all items in both array. However, if you
596+
> want to access elements of the wrapped collection, you need to use two
597+
> indices.
598+
599+
Consider the following example where the object `$myObject` has a property with
600+
single value and a property containing an array of 11 integers.
601+
602+
```powershell
603+
$myObject = [pscustomobject]@{
604+
singleValue = 'Hello'
605+
arrayValue = @(0..10)
606+
}
607+
```
608+
609+
When you use the `ForEach()` method to access a property of the object, the
610+
property is wrapped in a collection.
611+
612+
```powershell
613+
PS> $myObject.ForEach('singleValue').GetType().Name
614+
Collection`1
615+
PS> $myObject.ForEach('singleValue')[0].GetType().Name
616+
String
617+
PS> $myObject.ForEach('singleValue') # Enumerate the collection object
618+
Hello
619+
```
620+
621+
To access the an element of the array, you need to use two indices.
622+
623+
```powershell
624+
PS> $myObject.ForEach('arrayValue').GetType().Name
625+
Collection`1
626+
# A single Collection item
627+
PS> $myObject.ForEach('arrayValue').Count
628+
1
629+
# First item in the collection is an array of 11 items
630+
PS> $myObject.ForEach('Value')[0].Count
631+
11
632+
# Access the first item in the array of 11 items
633+
PS> $myObject.ForEach('Value')[0][0]
634+
0
635+
```
636+
637+
This is different than using the `ForEach()` method using with a scriptblock to
638+
access the **Value** property of each object.
639+
640+
```powershell
641+
PS> $myObject.ForEach({$_.Value}).Count # An array of 11 items
642+
11
643+
```
644+
645+
Use the scriptblock syntax to avoid the wrapping behavior when you want to
646+
access complex property types, such as arrays or nested objects.
647+
593648
#### ForEach(string methodName)
594649

595650
#### ForEach(string methodName, object[] arguments)
596651

597-
Lastly, `ForEach()` methods can be used to execute a method on every item in
598-
the collection.
652+
You can use the `ForEach()` method to execute an object's method on every item
653+
in the collection.
599654

600655
```powershell
601656
("one", "two", "three").ForEach("ToUpper")

0 commit comments

Comments
 (0)