Skip to content

Commit 059ce04

Browse files
authored
Clarify facts about intrinsic properties Count and Length (#11626)
1 parent 98f2518 commit 059ce04

File tree

9 files changed

+276
-239
lines changed

9 files changed

+276
-239
lines changed

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

Lines changed: 28 additions & 10 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: 03/07/2024
4+
ms.date: 01/03/2025
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
@@ -10,6 +10,7 @@ title: about_Arrays
1010
# about_Arrays
1111

1212
## Short description
13+
1314
Describes arrays, which are data structures designed to store collections of
1415
items.
1516

@@ -341,9 +342,25 @@ while($i -lt 4) {
341342

342343
### Count or Length or LongLength
343344

344-
To determine how many items are in an array, use the **Length** property or its
345-
**Count** alias. **Longlength** is useful if the array contains more than
346-
2,147,483,647 elements.
345+
In PowerShell, arrays have three properties that indicate the number of items
346+
contained in the array.
347+
348+
- **Count** - This property is the most commonly used property to determine the
349+
number of items in any collection, not just an array. It's an `[Int32]` type
350+
value. In Windows PowerShell 5.1 (and older) **Count** alias property for
351+
**Length**.
352+
353+
- **Length** - This property is an `[Int32]` type value. This contains the same
354+
value as **Count**.
355+
356+
> [!NOTE]
357+
> While **Count** and **Length** are equivalent for arrays, **Length** can
358+
> have a different meaning for other types. For example, **Length** for a
359+
> string is the number of characters in the string. But the **Count**
360+
> property is always `1`.
361+
362+
- **Longlength** - This property is an `[Int64]` type value. Use this property
363+
for arrays containing more than 2,147,483,647 elements.
347364

348365
```powershell
349366
$a = 0..9
@@ -470,7 +487,7 @@ True
470487
In this example, `$intA` is explicitly typed to contain integers.
471488

472489
```powershell
473-
[int[]] $intA = 1, 2, 3
490+
[Int[]] $intA = 1, 2, 3
474491
$intA.Clear()
475492
$intA
476493
```
@@ -488,7 +505,7 @@ for each element of the array.
488505

489506
The `ForEach()` method has several overloads that perform different operations.
490507

491-
```
508+
```Syntax
492509
ForEach(scriptblock expression)
493510
ForEach(scriptblock expression, object[] arguments)
494511
ForEach(type convertToType)
@@ -884,10 +901,11 @@ faster, especially for large arrays.
884901

885902
## Arrays of zero or one
886903

887-
Beginning in Windows PowerShell 3.0, a collection of zero or one object has the
888-
**Count** and **Length** properties. Also, you can index into an array of one
889-
object. This feature helps you to avoid scripting errors that occur when a
890-
command that expects a collection gets fewer than two items.
904+
Beginning in Windows PowerShell 3.0, a scalar types and collection of zero or
905+
one objects has the **Count** and **Length** properties. Also, you can use
906+
array index notation to access the value of a singleton scalar object. This
907+
feature helps you to avoid scripting errors that occur when a command that
908+
expects a collection gets fewer than two items.
891909

892910
> [!NOTE]
893911
In Windows PowerShell, objects created by casting a **Hashtable** to

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes automatic members in all PowerShell objects
33
Locale: en-US
4-
ms.date: 01/10/2024
4+
ms.date: 01/03/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_Intrinsic_Members?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Intrinsic_Members
@@ -167,8 +167,15 @@ information on how to use these methods, see [about_Arrays][01].
167167

168168
## Properties
169169

170-
The **Count** and **Length** properties are available to all PowerShell
171-
objects, not just collections. These are similar to each other but may work
170+
Not all scalar type have **Count** or **Length** properties in the base type.
171+
PowerShell adds the missing property as an intrinsic member for all scalar
172+
types.
173+
174+
> [!NOTE]
175+
> Uninitialized variables are implicitly `$null`. `$null` is scalar and has an
176+
> intrinsic **Count** and **Length** of 0.
177+
178+
While the **Count** and **Length** properties are similar, they may work
172179
differently depending on the data type. For example, the **Length** of a string
173180
is the number of characters in the string. The **Count** property is the number
174181
of instances of the object.

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

Lines changed: 50 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
---
22
description: Describes how to use object properties in PowerShell.
33
Locale: en-US
4-
ms.date: 08/21/2023
4+
ms.date: 01/03/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_properties?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Properties
88
---
99
# about_Properties
1010

1111
## Short description
12+
1213
Describes how to use object properties in PowerShell.
1314

1415
## Long description
@@ -32,7 +33,7 @@ it doesn't. A **DirectoryInfo** object, which represents a file system
3233
directory, has a **Parent** property that contains the path to the parent
3334
directory.
3435

35-
### Object properties
36+
## Object properties
3637

3738
To get the properties of an object, use the `Get-Member` cmdlet. For example,
3839
to get the properties of a **FileInfo** object, use the `Get-ChildItem` cmdlet
@@ -117,7 +118,7 @@ $a.CreationTime
117118
```
118119

119120
```Output
120-
Saturday, June 5, 2021 7:07:00 AM
121+
Wednesday, January 24, 2024 1:18:29 AM
121122
```
122123

123124
You can also use the `Select-Object` and `Format-List` cmdlets to display the
@@ -144,10 +145,10 @@ Mode : -a----
144145
VersionInfo : File: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
145146
InternalName: POWERSHELL
146147
OriginalFilename: PowerShell.EXE.MUI
147-
FileVersion: 10.0.22000.1 (WinBuild.160101.0800)
148+
FileVersion: 10.0.22621.1 (WinBuild.160101.0800)
148149
FileDescription: Windows PowerShell
149-
Product: Microsoft? Windows? Operating System
150-
ProductVersion: 10.0.22000.1
150+
Product: Microsoft® Windows® Operating System
151+
ProductVersion: 10.0.22621.1
151152
Debug: False
152153
Patched: False
153154
PreRelease: False
@@ -156,8 +157,8 @@ VersionInfo : File: C:\Windows\System32\WindowsPowerShell\v1.0
156157
Language: English (United States)
157158
158159
BaseName : powershell
159-
Target : {C:\Windows\WinSxS\amd64_microsoft-windows-powershell-exe_31bf3856ad364e35_10.0.22000.1_none_bf599c
160-
5a06fbb6f4\powershell.exe}
160+
Target : {C:\Windows\WinSxS\amd64_microsoft-windows-powershell-exe_31bf3856ad364e35_10.0.22621.3085_none_492
161+
e8ee57da24e0e\powershell.exe}
161162
LinkType : HardLink
162163
Name : powershell.exe
163164
Length : 450560
@@ -167,20 +168,20 @@ IsReadOnly : False
167168
Exists : True
168169
FullName : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
169170
Extension : .exe
170-
CreationTime : 6/5/2021 7:07:00 AM
171-
CreationTimeUtc : 6/5/2021 12:07:00 PM
172-
LastAccessTime : 7/18/2022 12:16:17 PM
173-
LastAccessTimeUtc : 7/18/2022 5:16:17 PM
174-
LastWriteTime : 6/5/2021 7:07:00 AM
175-
LastWriteTimeUtc : 6/5/2021 12:07:00 PM
171+
CreationTime : 1/24/2024 1:18:29 AM
172+
CreationTimeUtc : 1/24/2024 7:18:29 AM
173+
LastAccessTime : 1/3/2025 1:36:20 PM
174+
LastAccessTimeUtc : 1/3/2025 7:36:20 PM
175+
LastWriteTime : 1/24/2024 1:18:29 AM
176+
LastWriteTimeUtc : 1/24/2024 7:18:29 AM
176177
Attributes : Archive
177178
```
178179

179-
### Static properties
180+
## Static properties
180181

181182
You can use the static properties of .NET classes in PowerShell. Static
182-
properties are properties of class, unlike standard properties, which are
183-
properties of an object.
183+
properties are properties of the class, unlike standard properties, which are
184+
properties of an object instance.
184185

185186
To get the static properties of a class, use the **Static** parameter of the
186187
`Get-Member` cmdlet. For example, the following command gets the static
@@ -218,12 +219,8 @@ property of the `System.DateTime` class.
218219
## Member-access enumeration
219220

220221
Starting in PowerShell 3.0, when you use the member-access operator (`.`) to
221-
access a property that doesn't exist on a list collection, PowerShell
222-
automatically enumerates the items in the collection and returns the value of
223-
the property on each item. For more information, see
224-
[about_Member-Access_Enumeration](about_Member-Access_Enumeration.md).
225-
226-
### Examples
222+
access a property that doesn't exist, PowerShell automatically enumerates the
223+
items in the collection and returns the value of the property for each item.
227224

228225
This command returns the value of the **DisplayName** property of every service
229226
that `Get-Service` returns.
@@ -241,8 +238,8 @@ Application Information
241238
...
242239
```
243240

244-
All collections have a **Count** property that returns the number of objects in
245-
the collection.
241+
Most collections in PowerShell have a **Count** property that returns the
242+
number items in the collection.
246243

247244
```powershell
248245
(Get-Service).Count
@@ -252,50 +249,40 @@ the collection.
252249
176
253250
```
254251

255-
Starting in PowerShell 3.0, you can get the **Count** or **Length** property of
256-
singleton objects that aren't collections.
252+
If a property exists on the individual objects and on the collection, only the
253+
collection's property is returned.
257254

258255
```powershell
259-
(Get-Service Audiosrv).Count
260-
```
261-
262-
```Output
263-
1
264-
```
256+
PS> $collection = @(
257+
[pscustomobject]@{Length = "foo"}
258+
[pscustomobject]@{Length = "bar"}
259+
)
265260
266-
However, some objects have a **Length** property. For example, the **Length**
267-
of a string is the number of characters in the string. The **Count** property
268-
is the number of instances of the object.
261+
# PowerShell returns the collection's Length.
262+
$collection.Length
263+
2
269264
270-
```powershell
271-
PS> $str = 'string'
272-
PS> $str.Length
273-
6
274-
PS> $str.Count
275-
1
265+
# Get the length property of each item in the collection.
266+
PS> $collection.GetEnumerator().Length
267+
foo
268+
bar
276269
```
277270

278-
If a property exists on the individual objects and on the collection, only the
279-
collection's property is returned.
280-
281-
```powershell
282-
$collection = @(
283-
[pscustomobject]@{length = "foo"}
284-
[pscustomobject]@{length = "bar"}
285-
)
286-
# PowerShell returns the collection's Length.
287-
$collection.length
288-
```
289-
290-
```Output
291-
2
292-
```
271+
For more information, see [about_Member-Access_Enumeration][01].
293272

294273
## See also
295274

296-
- [about_Objects](about_Objects.md)
297-
- [about_Member-Access_Enumeration](about_Member-Access_Enumeration.md)
298-
- [about_Methods](about_Methods.md)
299-
- [Format-List](xref:Microsoft.PowerShell.Utility.Format-List)
300-
- [Get-Member](xref:Microsoft.PowerShell.Utility.Get-Member)
301-
- [Select-Object](xref:Microsoft.PowerShell.Utility.Select-Object)
275+
- [about_Objects][03]
276+
- [about_Member-Access_Enumeration][01]
277+
- [about_Methods][02]
278+
- [Format-List][04]
279+
- [Get-Member][05]
280+
- [Select-Object][06]
281+
282+
<!-- link references -->
283+
[01]: about_Member-Access_Enumeration.md
284+
[02]: about_Methods.md
285+
[03]: about_Objects.md
286+
[04]: xref:Microsoft.PowerShell.Utility.Format-List
287+
[05]: xref:Microsoft.PowerShell.Utility.Get-Member
288+
[06]: xref:Microsoft.PowerShell.Utility.Select-Object

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

Lines changed: 29 additions & 11 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: 03/07/2024
4+
ms.date: 01/03/2025
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.4&WT.mc_id=ps-gethelp
77
schema: 2.0.0
@@ -10,6 +10,7 @@ title: about_Arrays
1010
# about_Arrays
1111

1212
## Short description
13+
1314
Describes arrays, which are data structures designed to store collections of
1415
items.
1516

@@ -67,7 +68,7 @@ values of a particular type, cast the variable as an array type, such as
6768
variable name with an array type enclosed in brackets. For example:
6869

6970
```powershell
70-
[int32[]]$ia = 1500, 2230, 3350, 4000
71+
[Int32[]]$ia = 1500, 2230, 3350, 4000
7172
```
7273

7374
As a result, the `$ia` array can contain only integers.
@@ -341,9 +342,25 @@ while($i -lt 4) {
341342

342343
### Count or Length or LongLength
343344

344-
To determine how many items are in an array, use the **Length** property or its
345-
**Count** alias. **Longlength** is useful if the array contains more than
346-
2,147,483,647 elements.
345+
In PowerShell, arrays have three properties that indicate the number of items
346+
contained in the array.
347+
348+
- **Count** - This property is the most commonly used property to determine the
349+
number of items in any collection, not just an array. It's an `[Int32]` type
350+
value. In Windows PowerShell 5.1 (and older) **Count** alias property for
351+
**Length**.
352+
353+
- **Length** - This property is an `[Int32]` type value. This contains the same
354+
value as **Count**.
355+
356+
> [!NOTE]
357+
> While **Count** and **Length** are equivalent for arrays, **Length** can
358+
> have a different meaning for other types. For example, **Length** for a
359+
> string is the number of characters in the string. But the **Count**
360+
> property is always `1`.
361+
362+
- **Longlength** - This property is an `[Int64]` type value. Use this property
363+
for arrays containing more than 2,147,483,647 elements.
347364

348365
```powershell
349366
$a = 0..9
@@ -470,7 +487,7 @@ True
470487
In this example, `$intA` is explicitly typed to contain integers.
471488

472489
```powershell
473-
[int[]] $intA = 1, 2, 3
490+
[Int[]] $intA = 1, 2, 3
474491
$intA.Clear()
475492
$intA
476493
```
@@ -488,7 +505,7 @@ for each element of the array.
488505

489506
The `ForEach()` method has several overloads that perform different operations.
490507

491-
```
508+
```Syntax
492509
ForEach(scriptblock expression)
493510
ForEach(scriptblock expression, object[] arguments)
494511
ForEach(type convertToType)
@@ -884,10 +901,11 @@ faster, especially for large arrays.
884901

885902
## Arrays of zero or one
886903

887-
Beginning in Windows PowerShell 3.0, a collection of zero or one object has the
888-
**Count** and **Length** properties. Also, you can index into an array of one
889-
object. This feature helps you to avoid scripting errors that occur when a
890-
command that expects a collection gets fewer than two items.
904+
Beginning in Windows PowerShell 3.0, a scalar types and collection of zero or
905+
one objects has the **Count** and **Length** properties. Also, you can use
906+
array index notation to access the value of a singleton scalar object. This
907+
feature helps you to avoid scripting errors that occur when a command that
908+
expects a collection gets fewer than two items.
891909

892910
The following example shows that a variable that contains no objects has a
893911
**Count** and **Length** of 0.

0 commit comments

Comments
 (0)