diff --git a/reference/5.1/Microsoft.PowerShell.Core/About/about_Arrays.md b/reference/5.1/Microsoft.PowerShell.Core/About/about_Arrays.md index 3229c0dc5cbd..6f67cef704f0 100644 --- a/reference/5.1/Microsoft.PowerShell.Core/About/about_Arrays.md +++ b/reference/5.1/Microsoft.PowerShell.Core/About/about_Arrays.md @@ -1,7 +1,7 @@ --- description: Describes arrays, which are data structures designed to store collections of items. Locale: en-US -ms.date: 03/07/2024 +ms.date: 01/03/2025 no-loc: [Count, Length, LongLength, Rank, ForEach, Clear, Default, First, Last, SkipUntil, Until, Split, Tuple] online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-5.1&WT.mc_id=ps-gethelp schema: 2.0.0 @@ -10,6 +10,7 @@ title: about_Arrays # about_Arrays ## Short description + Describes arrays, which are data structures designed to store collections of items. @@ -341,9 +342,25 @@ while($i -lt 4) { ### Count or Length or LongLength -To determine how many items are in an array, use the **Length** property or its -**Count** alias. **Longlength** is useful if the array contains more than -2,147,483,647 elements. +In PowerShell, arrays have three properties that indicate the number of items +contained in the array. + +- **Count** - This property is the most commonly used property to determine the + number of items in any collection, not just an array. It's an `[Int32]` type + value. In Windows PowerShell 5.1 (and older) **Count** alias property for + **Length**. + +- **Length** - This property is an `[Int32]` type value. This contains the same + value as **Count**. + + > [!NOTE] + > While **Count** and **Length** are equivalent for arrays, **Length** can + > have a different meaning for other types. For example, **Length** for a + > string is the number of characters in the string. But the **Count** + > property is always `1`. + +- **Longlength** - This property is an `[Int64]` type value. Use this property + for arrays containing more than 2,147,483,647 elements. ```powershell $a = 0..9 @@ -470,7 +487,7 @@ True In this example, `$intA` is explicitly typed to contain integers. ```powershell -[int[]] $intA = 1, 2, 3 +[Int[]] $intA = 1, 2, 3 $intA.Clear() $intA ``` @@ -488,7 +505,7 @@ for each element of the array. The `ForEach()` method has several overloads that perform different operations. -``` +```Syntax ForEach(scriptblock expression) ForEach(scriptblock expression, object[] arguments) ForEach(type convertToType) @@ -884,10 +901,11 @@ faster, especially for large arrays. ## Arrays of zero or one -Beginning in Windows PowerShell 3.0, a collection of zero or one object has the -**Count** and **Length** properties. Also, you can index into an array of one -object. This feature helps you to avoid scripting errors that occur when a -command that expects a collection gets fewer than two items. +Beginning in Windows PowerShell 3.0, a scalar types and collection of zero or +one objects has the **Count** and **Length** properties. Also, you can use +array index notation to access the value of a singleton scalar object. This +feature helps you to avoid scripting errors that occur when a command that +expects a collection gets fewer than two items. > [!NOTE] In Windows PowerShell, objects created by casting a **Hashtable** to diff --git a/reference/5.1/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md b/reference/5.1/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md index 68c07f4b0a5d..41bea085e39a 100644 --- a/reference/5.1/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md +++ b/reference/5.1/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md @@ -1,7 +1,7 @@ --- description: Describes automatic members in all PowerShell objects Locale: en-US -ms.date: 01/10/2024 +ms.date: 01/03/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_Intrinsic_Members?view=powershell-5.1&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Intrinsic_Members @@ -167,8 +167,15 @@ information on how to use these methods, see [about_Arrays][01]. ## Properties -The **Count** and **Length** properties are available to all PowerShell -objects, not just collections. These are similar to each other but may work +Not all scalar type have **Count** or **Length** properties in the base type. +PowerShell adds the missing property as an intrinsic member for all scalar +types. + +> [!NOTE] +> Uninitialized variables are implicitly `$null`. `$null` is scalar and has an +> intrinsic **Count** and **Length** of 0. + +While the **Count** and **Length** properties are similar, they may work differently depending on the data type. For example, the **Length** of a string is the number of characters in the string. The **Count** property is the number of instances of the object. diff --git a/reference/5.1/Microsoft.PowerShell.Core/About/about_Properties.md b/reference/5.1/Microsoft.PowerShell.Core/About/about_Properties.md index 7498cac854df..2a8bda5c9234 100644 --- a/reference/5.1/Microsoft.PowerShell.Core/About/about_Properties.md +++ b/reference/5.1/Microsoft.PowerShell.Core/About/about_Properties.md @@ -1,7 +1,7 @@ --- description: Describes how to use object properties in PowerShell. Locale: en-US -ms.date: 08/21/2023 +ms.date: 01/03/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_properties?view=powershell-5.1&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Properties @@ -9,6 +9,7 @@ title: about_Properties # about_Properties ## Short description + Describes how to use object properties in PowerShell. ## Long description @@ -32,7 +33,7 @@ it doesn't. A **DirectoryInfo** object, which represents a file system directory, has a **Parent** property that contains the path to the parent directory. -### Object properties +## Object properties To get the properties of an object, use the `Get-Member` cmdlet. For example, to get the properties of a **FileInfo** object, use the `Get-ChildItem` cmdlet @@ -117,7 +118,7 @@ $a.CreationTime ``` ```Output -Saturday, June 5, 2021 7:07:00 AM +Wednesday, January 24, 2024 1:18:29 AM ``` You can also use the `Select-Object` and `Format-List` cmdlets to display the @@ -144,10 +145,10 @@ Mode : -a---- VersionInfo : File: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe InternalName: POWERSHELL OriginalFilename: PowerShell.EXE.MUI - FileVersion: 10.0.22000.1 (WinBuild.160101.0800) + FileVersion: 10.0.22621.1 (WinBuild.160101.0800) FileDescription: Windows PowerShell - Product: Microsoft? Windows? Operating System - ProductVersion: 10.0.22000.1 + Product: Microsoft® Windows® Operating System + ProductVersion: 10.0.22621.1 Debug: False Patched: False PreRelease: False @@ -156,8 +157,8 @@ VersionInfo : File: C:\Windows\System32\WindowsPowerShell\v1.0 Language: English (United States) BaseName : powershell -Target : {C:\Windows\WinSxS\amd64_microsoft-windows-powershell-exe_31bf3856ad364e35_10.0.22000.1_none_bf599c - 5a06fbb6f4\powershell.exe} +Target : {C:\Windows\WinSxS\amd64_microsoft-windows-powershell-exe_31bf3856ad364e35_10.0.22621.3085_none_492 + e8ee57da24e0e\powershell.exe} LinkType : HardLink Name : powershell.exe Length : 450560 @@ -167,20 +168,20 @@ IsReadOnly : False Exists : True FullName : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Extension : .exe -CreationTime : 6/5/2021 7:07:00 AM -CreationTimeUtc : 6/5/2021 12:07:00 PM -LastAccessTime : 7/18/2022 12:16:17 PM -LastAccessTimeUtc : 7/18/2022 5:16:17 PM -LastWriteTime : 6/5/2021 7:07:00 AM -LastWriteTimeUtc : 6/5/2021 12:07:00 PM +CreationTime : 1/24/2024 1:18:29 AM +CreationTimeUtc : 1/24/2024 7:18:29 AM +LastAccessTime : 1/3/2025 1:36:20 PM +LastAccessTimeUtc : 1/3/2025 7:36:20 PM +LastWriteTime : 1/24/2024 1:18:29 AM +LastWriteTimeUtc : 1/24/2024 7:18:29 AM Attributes : Archive ``` -### Static properties +## Static properties You can use the static properties of .NET classes in PowerShell. Static -properties are properties of class, unlike standard properties, which are -properties of an object. +properties are properties of the class, unlike standard properties, which are +properties of an object instance. To get the static properties of a class, use the **Static** parameter of the `Get-Member` cmdlet. For example, the following command gets the static @@ -218,12 +219,8 @@ property of the `System.DateTime` class. ## Member-access enumeration Starting in PowerShell 3.0, when you use the member-access operator (`.`) to -access a property that doesn't exist on a list collection, PowerShell -automatically enumerates the items in the collection and returns the value of -the property on each item. For more information, see -[about_Member-Access_Enumeration](about_Member-Access_Enumeration.md). - -### Examples +access a property that doesn't exist, PowerShell automatically enumerates the +items in the collection and returns the value of the property for each item. This command returns the value of the **DisplayName** property of every service that `Get-Service` returns. @@ -241,8 +238,8 @@ Application Information ... ``` -All collections have a **Count** property that returns the number of objects in -the collection. +Most collections in PowerShell have a **Count** property that returns the +number items in the collection. ```powershell (Get-Service).Count @@ -252,50 +249,40 @@ the collection. 176 ``` -Starting in PowerShell 3.0, you can get the **Count** or **Length** property of -singleton objects that aren't collections. +If a property exists on the individual objects and on the collection, only the +collection's property is returned. ```powershell -(Get-Service Audiosrv).Count -``` - -```Output -1 -``` +PS> $collection = @( + [pscustomobject]@{Length = "foo"} + [pscustomobject]@{Length = "bar"} +) -However, some objects have a **Length** property. For example, the **Length** -of a string is the number of characters in the string. The **Count** property -is the number of instances of the object. +# PowerShell returns the collection's Length. +$collection.Length +2 -```powershell -PS> $str = 'string' -PS> $str.Length -6 -PS> $str.Count -1 +# Get the length property of each item in the collection. +PS> $collection.GetEnumerator().Length +foo +bar ``` -If a property exists on the individual objects and on the collection, only the -collection's property is returned. - - ```powershell - $collection = @( - [pscustomobject]@{length = "foo"} - [pscustomobject]@{length = "bar"} -) - # PowerShell returns the collection's Length. - $collection.length - ``` - - ```Output - 2 - ``` +For more information, see [about_Member-Access_Enumeration][01]. ## See also -- [about_Objects](about_Objects.md) -- [about_Member-Access_Enumeration](about_Member-Access_Enumeration.md) -- [about_Methods](about_Methods.md) -- [Format-List](xref:Microsoft.PowerShell.Utility.Format-List) -- [Get-Member](xref:Microsoft.PowerShell.Utility.Get-Member) -- [Select-Object](xref:Microsoft.PowerShell.Utility.Select-Object) +- [about_Objects][03] +- [about_Member-Access_Enumeration][01] +- [about_Methods][02] +- [Format-List][04] +- [Get-Member][05] +- [Select-Object][06] + + +[01]: about_Member-Access_Enumeration.md +[02]: about_Methods.md +[03]: about_Objects.md +[04]: xref:Microsoft.PowerShell.Utility.Format-List +[05]: xref:Microsoft.PowerShell.Utility.Get-Member +[06]: xref:Microsoft.PowerShell.Utility.Select-Object diff --git a/reference/7.4/Microsoft.PowerShell.Core/About/about_Arrays.md b/reference/7.4/Microsoft.PowerShell.Core/About/about_Arrays.md index 03385f12ad3c..1c58b7a48c4e 100644 --- a/reference/7.4/Microsoft.PowerShell.Core/About/about_Arrays.md +++ b/reference/7.4/Microsoft.PowerShell.Core/About/about_Arrays.md @@ -1,7 +1,7 @@ --- description: Describes arrays, which are data structures designed to store collections of items. Locale: en-US -ms.date: 03/07/2024 +ms.date: 01/03/2025 no-loc: [Count, Length, LongLength, Rank, ForEach, Clear, Default, First, Last, SkipUntil, Until, Split, Tuple] online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7.4&WT.mc_id=ps-gethelp schema: 2.0.0 @@ -10,6 +10,7 @@ title: about_Arrays # about_Arrays ## Short description + Describes arrays, which are data structures designed to store collections of items. @@ -67,7 +68,7 @@ values of a particular type, cast the variable as an array type, such as variable name with an array type enclosed in brackets. For example: ```powershell -[int32[]]$ia = 1500, 2230, 3350, 4000 +[Int32[]]$ia = 1500, 2230, 3350, 4000 ``` As a result, the `$ia` array can contain only integers. @@ -341,9 +342,25 @@ while($i -lt 4) { ### Count or Length or LongLength -To determine how many items are in an array, use the **Length** property or its -**Count** alias. **Longlength** is useful if the array contains more than -2,147,483,647 elements. +In PowerShell, arrays have three properties that indicate the number of items +contained in the array. + +- **Count** - This property is the most commonly used property to determine the + number of items in any collection, not just an array. It's an `[Int32]` type + value. In Windows PowerShell 5.1 (and older) **Count** alias property for + **Length**. + +- **Length** - This property is an `[Int32]` type value. This contains the same + value as **Count**. + + > [!NOTE] + > While **Count** and **Length** are equivalent for arrays, **Length** can + > have a different meaning for other types. For example, **Length** for a + > string is the number of characters in the string. But the **Count** + > property is always `1`. + +- **Longlength** - This property is an `[Int64]` type value. Use this property + for arrays containing more than 2,147,483,647 elements. ```powershell $a = 0..9 @@ -470,7 +487,7 @@ True In this example, `$intA` is explicitly typed to contain integers. ```powershell -[int[]] $intA = 1, 2, 3 +[Int[]] $intA = 1, 2, 3 $intA.Clear() $intA ``` @@ -488,7 +505,7 @@ for each element of the array. The `ForEach()` method has several overloads that perform different operations. -``` +```Syntax ForEach(scriptblock expression) ForEach(scriptblock expression, object[] arguments) ForEach(type convertToType) @@ -884,10 +901,11 @@ faster, especially for large arrays. ## Arrays of zero or one -Beginning in Windows PowerShell 3.0, a collection of zero or one object has the -**Count** and **Length** properties. Also, you can index into an array of one -object. This feature helps you to avoid scripting errors that occur when a -command that expects a collection gets fewer than two items. +Beginning in Windows PowerShell 3.0, a scalar types and collection of zero or +one objects has the **Count** and **Length** properties. Also, you can use +array index notation to access the value of a singleton scalar object. This +feature helps you to avoid scripting errors that occur when a command that +expects a collection gets fewer than two items. The following example shows that a variable that contains no objects has a **Count** and **Length** of 0. diff --git a/reference/7.4/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md b/reference/7.4/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md index d29b594d4143..246f7695617a 100644 --- a/reference/7.4/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md +++ b/reference/7.4/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md @@ -1,7 +1,7 @@ --- description: Describes automatic members in all PowerShell objects Locale: en-US -ms.date: 01/10/2024 +ms.date: 01/03/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_Intrinsic_Members?view=powershell-7.4&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Intrinsic_Members @@ -167,8 +167,15 @@ information on how to use these methods, see [about_Arrays][01]. ## Properties -The **Count** and **Length** properties are available to all PowerShell -objects, not just collections. These are similar to each other but may work +Not all scalar type have **Count** or **Length** properties in the base type. +PowerShell adds the missing property as an intrinsic member for all scalar +types. + +> [!NOTE] +> Uninitialized variables are implicitly `$null`. `$null` is scalar and has an +> intrinsic **Count** and **Length** of 0. + +While the **Count** and **Length** properties are similar, they may work differently depending on the data type. For example, the **Length** of a string is the number of characters in the string. The **Count** property is the number of instances of the object. diff --git a/reference/7.4/Microsoft.PowerShell.Core/About/about_Properties.md b/reference/7.4/Microsoft.PowerShell.Core/About/about_Properties.md index 4956b646e6d5..40e40e6727f6 100644 --- a/reference/7.4/Microsoft.PowerShell.Core/About/about_Properties.md +++ b/reference/7.4/Microsoft.PowerShell.Core/About/about_Properties.md @@ -1,7 +1,7 @@ --- description: Describes how to use object properties in PowerShell. Locale: en-US -ms.date: 08/21/2023 +ms.date: 01/03/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_properties?view=powershell-7.4&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Properties @@ -9,6 +9,7 @@ title: about_Properties # about_Properties ## Short description + Describes how to use object properties in PowerShell. ## Long description @@ -32,7 +33,7 @@ it doesn't. A **DirectoryInfo** object, which represents a file system directory, has a **Parent** property that contains the path to the parent directory. -### Object properties +## Object properties To get the properties of an object, use the `Get-Member` cmdlet. For example, to get the properties of a **FileInfo** object, use the `Get-ChildItem` cmdlet @@ -116,7 +117,7 @@ $a.CreationTime ``` ```Output -Tuesday, June 14, 2022 5:17:14 PM +Wednesday, October 16, 2024 4:51:56 PM ``` You can also use the `Select-Object` and `Format-List` cmdlets to display the @@ -133,21 +134,22 @@ Get-ChildItem $PSHOME\pwsh.exe | Format-List -Property * ``` ```Output -PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7-preview\pwsh.exe -PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7-preview +PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7\pwsh.exe +PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7 PSChildName : pwsh.exe PSDrive : C PSProvider : Microsoft.PowerShell.Core\FileSystem PSIsContainer : False Mode : -a--- ModeWithoutHardLink : -a--- -VersionInfo : File: C:\Program Files\PowerShell\7-preview\pwsh.exe +VersionInfo : File: C:\Program Files\PowerShell\7\pwsh.exe InternalName: pwsh.dll OriginalFilename: pwsh.dll - FileVersion: 7.3.0.5 + FileVersion: 7.4.6.500 FileDescription: pwsh Product: PowerShell - ProductVersion: 7.3.0-preview.5 SHA: cfc237ac85cf24fa760923ace568201c8f3256aa + ProductVersion: 7.4.6 SHA: + d71d4f122db89c1bcfb5571b9445d600803c332b+d71d4f122db89c1bcfb5571b9445d600803c332b Debug: False Patched: False PreRelease: False @@ -156,32 +158,33 @@ VersionInfo : File: C:\Program Files\PowerShell\7-preview\pw Language: Language Neutral BaseName : pwsh -ResolvedTarget : C:\Program Files\PowerShell\7-preview\pwsh.exe +ResolvedTarget : C:\Program Files\PowerShell\7\pwsh.exe Target : LinkType : -Length : 285088 -DirectoryName : C:\Program Files\PowerShell\7-preview -Directory : C:\Program Files\PowerShell\7-preview -IsReadOnly : False -FullName : C:\Program Files\PowerShell\7-preview\pwsh.exe -Extension : .exe Name : pwsh.exe +Length : 278048 +DirectoryName : C:\Program Files\PowerShell\7 +Directory : C:\Program Files\PowerShell\7 +IsReadOnly : False Exists : True -CreationTime : 6/14/2022 5:17:14 PM -CreationTimeUtc : 6/14/2022 10:17:14 PM -LastAccessTime : 7/18/2022 11:32:06 AM -LastAccessTimeUtc : 7/18/2022 4:32:06 PM -LastWriteTime : 6/14/2022 5:17:14 PM -LastWriteTimeUtc : 6/14/2022 10:17:14 PM +FullName : C:\Program Files\PowerShell\7\pwsh.exe +Extension : .exe +CreationTime : 10/16/2024 4:51:56 PM +CreationTimeUtc : 10/16/2024 9:51:56 PM +LastAccessTime : 1/3/2025 1:33:10 PM +LastAccessTimeUtc : 1/3/2025 7:33:10 PM +LastWriteTime : 10/16/2024 4:51:56 PM +LastWriteTimeUtc : 10/16/2024 9:51:56 PM LinkTarget : +UnixFileMode : -1 Attributes : Archive ``` -### Static properties +## Static properties You can use the static properties of .NET classes in PowerShell. Static -properties are properties of class, unlike standard properties, which are -properties of an object. +properties are properties of the class, unlike standard properties, which are +properties of an object instance. To get the static properties of a class, use the **Static** parameter of the `Get-Member` cmdlet. For example, the following command gets the static @@ -219,12 +222,8 @@ property of the `System.DateTime` class. ## Member-access enumeration Starting in PowerShell 3.0, when you use the member-access operator (`.`) to -access a property that doesn't exist on a list collection, PowerShell -automatically enumerates the items in the collection and returns the value of -the property on each item. For more information, see -[about_Member-Access_Enumeration](about_Member-Access_Enumeration.md). - -### Examples +access a property that doesn't exist, PowerShell automatically enumerates the +items in the collection and returns the value of the property for each item. This command returns the value of the **DisplayName** property of every service that `Get-Service` returns. @@ -242,8 +241,8 @@ Application Information ... ``` -All collections have a **Count** property that returns the number of objects in -the collection. +Most collections in PowerShell have a **Count** property that returns the +number items in the collection. ```powershell (Get-Service).Count @@ -253,50 +252,40 @@ the collection. 176 ``` -Starting in PowerShell 3.0, you can get the **Count** or **Length** property of -singleton objects that aren't collections. +If a property exists on the individual objects and on the collection, only the +collection's property is returned. ```powershell -(Get-Service Audiosrv).Count -``` - -```Output -1 -``` +PS> $collection = @( + [pscustomobject]@{Length = "foo"} + [pscustomobject]@{Length = "bar"} +) -However, some objects have a **Length** property. For example, the **Length** -of a string is the number of characters in the string. The **Count** property -is the number of instances of the object. +# PowerShell returns the collection's Length. +$collection.Length +2 -```powershell -PS> $str = 'string' -PS> $str.Length -6 -PS> $str.Count -1 +# Get the length property of each item in the collection. +PS> $collection.GetEnumerator().Length +foo +bar ``` -If a property exists on the individual objects and on the collection, only the -collection's property is returned. - - ```powershell - $collection = @( - [pscustomobject]@{length = "foo"} - [pscustomobject]@{length = "bar"} -) - # PowerShell returns the collection's Length. - $collection.length - ``` - - ```Output - 2 - ``` +For more information, see [about_Member-Access_Enumeration][01]. ## See also -- [about_Objects](about_Objects.md) -- [about_Member-Access_Enumeration](about_Member-Access_Enumeration.md) -- [about_Methods](about_Methods.md) -- [Format-List](xref:Microsoft.PowerShell.Utility.Format-List) -- [Get-Member](xref:Microsoft.PowerShell.Utility.Get-Member) -- [Select-Object](xref:Microsoft.PowerShell.Utility.Select-Object) +- [about_Objects][03] +- [about_Member-Access_Enumeration][01] +- [about_Methods][02] +- [Format-List][04] +- [Get-Member][05] +- [Select-Object][06] + + +[01]: about_Member-Access_Enumeration.md +[02]: about_Methods.md +[03]: about_Objects.md +[04]: xref:Microsoft.PowerShell.Utility.Format-List +[05]: xref:Microsoft.PowerShell.Utility.Get-Member +[06]: xref:Microsoft.PowerShell.Utility.Select-Object diff --git a/reference/7.5/Microsoft.PowerShell.Core/About/about_Arrays.md b/reference/7.5/Microsoft.PowerShell.Core/About/about_Arrays.md index 96a1733f4e4c..cd7cebefe88b 100644 --- a/reference/7.5/Microsoft.PowerShell.Core/About/about_Arrays.md +++ b/reference/7.5/Microsoft.PowerShell.Core/About/about_Arrays.md @@ -1,7 +1,7 @@ --- description: Describes arrays, which are data structures designed to store collections of items. Locale: en-US -ms.date: 03/07/2024 +ms.date: 01/03/2025 no-loc: [Count, Length, LongLength, Rank, ForEach, Clear, Default, First, Last, SkipUntil, Until, Split, Tuple] online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7.5&WT.mc_id=ps-gethelp schema: 2.0.0 @@ -68,7 +68,7 @@ values of a particular type, cast the variable as an array type, such as variable name with an array type enclosed in brackets. For example: ```powershell -[int32[]]$ia = 1500, 2230, 3350, 4000 +[Int32[]]$ia = 1500, 2230, 3350, 4000 ``` As a result, the `$ia` array can contain only integers. @@ -342,9 +342,25 @@ while($i -lt 4) { ### Count or Length or LongLength -To determine how many items are in an array, use the **Length** property or its -**Count** alias. **Longlength** is useful if the array contains more than -2,147,483,647 elements. +In PowerShell, arrays have three properties that indicate the number of items +contained in the array. + +- **Count** - This property is the most commonly used property to determine the + number of items in any collection, not just an array. It's an `[Int32]` type + value. In Windows PowerShell 5.1 (and older) **Count** alias property for + **Length**. + +- **Length** - This property is an `[Int32]` type value. This contains the same + value as **Count**. + + > [!NOTE] + > While **Count** and **Length** are equivalent for arrays, **Length** can + > have a different meaning for other types. For example, **Length** for a + > string is the number of characters in the string. But the **Count** + > property is always `1`. + +- **Longlength** - This property is an `[Int64]` type value. Use this property + for arrays containing more than 2,147,483,647 elements. ```powershell $a = 0..9 @@ -471,7 +487,7 @@ True In this example, `$intA` is explicitly typed to contain integers. ```powershell -[int[]] $intA = 1, 2, 3 +[Int[]] $intA = 1, 2, 3 $intA.Clear() $intA ``` @@ -489,7 +505,7 @@ for each element of the array. The `ForEach()` method has several overloads that perform different operations. -``` +```Syntax ForEach(scriptblock expression) ForEach(scriptblock expression, object[] arguments) ForEach(type convertToType) @@ -885,10 +901,11 @@ faster, especially for large arrays. ## Arrays of zero or one -Beginning in Windows PowerShell 3.0, a collection of zero or one object has the -**Count** and **Length** properties. Also, you can index into an array of one -object. This feature helps you to avoid scripting errors that occur when a -command that expects a collection gets fewer than two items. +Beginning in Windows PowerShell 3.0, a scalar types and collection of zero or +one objects has the **Count** and **Length** properties. Also, you can use +array index notation to access the value of a singleton scalar object. This +feature helps you to avoid scripting errors that occur when a command that +expects a collection gets fewer than two items. The following example shows that a variable that contains no objects has a **Count** and **Length** of 0. diff --git a/reference/7.5/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md b/reference/7.5/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md index 05acada77713..269825183622 100644 --- a/reference/7.5/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md +++ b/reference/7.5/Microsoft.PowerShell.Core/About/about_Intrinsic_Members.md @@ -1,7 +1,7 @@ --- description: Describes automatic members in all PowerShell objects Locale: en-US -ms.date: 01/10/2024 +ms.date: 01/03/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_Intrinsic_Members?view=powershell-7.5&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Intrinsic_Members @@ -167,8 +167,15 @@ information on how to use these methods, see [about_Arrays][01]. ## Properties -The **Count** and **Length** properties are available to all PowerShell -objects, not just collections. These are similar to each other but may work +Not all scalar type have **Count** or **Length** properties in the base type. +PowerShell adds the missing property as an intrinsic member for all scalar +types. + +> [!NOTE] +> Uninitialized variables are implicitly `$null`. `$null` is scalar and has an +> intrinsic **Count** and **Length** of 0. + +While the **Count** and **Length** properties are similar, they may work differently depending on the data type. For example, the **Length** of a string is the number of characters in the string. The **Count** property is the number of instances of the object. diff --git a/reference/7.5/Microsoft.PowerShell.Core/About/about_Properties.md b/reference/7.5/Microsoft.PowerShell.Core/About/about_Properties.md index b3b2b74fa7b8..28692aa044ac 100644 --- a/reference/7.5/Microsoft.PowerShell.Core/About/about_Properties.md +++ b/reference/7.5/Microsoft.PowerShell.Core/About/about_Properties.md @@ -1,7 +1,7 @@ --- description: Describes how to use object properties in PowerShell. Locale: en-US -ms.date: 08/21/2023 +ms.date: 01/03/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_properties?view=powershell-7.5&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Properties @@ -33,7 +33,7 @@ it doesn't. A **DirectoryInfo** object, which represents a file system directory, has a **Parent** property that contains the path to the parent directory. -### Object properties +## Object properties To get the properties of an object, use the `Get-Member` cmdlet. For example, to get the properties of a **FileInfo** object, use the `Get-ChildItem` cmdlet @@ -117,7 +117,7 @@ $a.CreationTime ``` ```Output -Tuesday, June 14, 2022 5:17:14 PM +Wednesday, November 13, 2024 10:12:26 PM ``` You can also use the `Select-Object` and `Format-List` cmdlets to display the @@ -145,36 +145,37 @@ ModeWithoutHardLink : -a--- VersionInfo : File: C:\Program Files\PowerShell\7-preview\pwsh.exe InternalName: pwsh.dll OriginalFilename: pwsh.dll - FileVersion: 7.3.0.5 - FileDescription: pwsh + FileVersion: 7.5.0.101 + FileDescription: PowerShell 7 Product: PowerShell - ProductVersion: 7.3.0-preview.5 SHA: cfc237ac85cf24fa760923ace568201c8f3256aa + ProductVersion: 7.5.0-rc.1 SHA: c0142dde17137e436e302b3c4e93e2d6dc50c5c4+c0142dde17137e436e302b3c4e93e2d6dc50c5c4 Debug: False Patched: False PreRelease: False PrivateBuild: False SpecialBuild: False Language: Language Neutral - + BaseName : pwsh ResolvedTarget : C:\Program Files\PowerShell\7-preview\pwsh.exe -Target : -LinkType : -Length : 285088 +Target : +LinkType : +Name : pwsh.exe +Length : 284704 DirectoryName : C:\Program Files\PowerShell\7-preview Directory : C:\Program Files\PowerShell\7-preview IsReadOnly : False +Exists : True FullName : C:\Program Files\PowerShell\7-preview\pwsh.exe Extension : .exe -Name : pwsh.exe -Exists : True -CreationTime : 6/14/2022 5:17:14 PM -CreationTimeUtc : 6/14/2022 10:17:14 PM -LastAccessTime : 7/18/2022 11:32:06 AM -LastAccessTimeUtc : 7/18/2022 4:32:06 PM -LastWriteTime : 6/14/2022 5:17:14 PM -LastWriteTimeUtc : 6/14/2022 10:17:14 PM -LinkTarget : +CreationTime : 11/13/2024 10:12:26 PM +CreationTimeUtc : 11/14/2024 4:12:26 AM +LastAccessTime : 1/3/2025 1:38:13 PM +LastAccessTimeUtc : 1/3/2025 7:38:13 PM +LastWriteTime : 11/13/2024 10:12:26 PM +LastWriteTimeUtc : 11/14/2024 4:12:26 AM +LinkTarget : +UnixFileMode : -1 Attributes : Archive ``` @@ -220,12 +221,8 @@ property of the `System.DateTime` class. ## Member-access enumeration Starting in PowerShell 3.0, when you use the member-access operator (`.`) to -access a property that doesn't exist on a list collection, PowerShell -automatically enumerates the items in the collection and returns the value of -the property on each item. For more information, see -[about_Member-Access_Enumeration](about_Member-Access_Enumeration.md). - -### Examples +access a property that doesn't exist, PowerShell automatically enumerates the +items in the collection and returns the value of the property for each item. This command returns the value of the **DisplayName** property of every service that `Get-Service` returns. @@ -243,8 +240,8 @@ Application Information ... ``` -All collections have a **Count** property that returns the number of objects in -the collection. +Most collections in PowerShell have a **Count** property that returns the +number items in the collection. ```powershell (Get-Service).Count @@ -254,50 +251,40 @@ the collection. 176 ``` -Starting in PowerShell 3.0, you can get the **Count** or **Length** property of -singleton objects that aren't collections. +If a property exists on the individual objects and on the collection, only the +collection's property is returned. ```powershell -(Get-Service Audiosrv).Count -``` - -```Output -1 -``` +PS> $collection = @( + [pscustomobject]@{Length = "foo"} + [pscustomobject]@{Length = "bar"} +) -However, some objects have a **Length** property. For example, the **Length** -of a string is the number of characters in the string. The **Count** property -is the number of instances of the object. +# PowerShell returns the collection's Length. +$collection.Length +2 -```powershell -PS> $str = 'string' -PS> $str.Length -6 -PS> $str.Count -1 +# Get the length property of each item in the collection. +PS> $collection.GetEnumerator().Length +foo +bar ``` -If a property exists on the individual objects and on the collection, only the -collection's property is returned. - - ```powershell - $collection = @( - [pscustomobject]@{length = "foo"} - [pscustomobject]@{length = "bar"} -) - # PowerShell returns the collection's Length. - $collection.length - ``` - - ```Output - 2 - ``` +For more information, see [about_Member-Access_Enumeration][01]. ## See also -- [about_Objects](about_Objects.md) -- [about_Member-Access_Enumeration](about_Member-Access_Enumeration.md) -- [about_Methods](about_Methods.md) -- [Format-List](xref:Microsoft.PowerShell.Utility.Format-List) -- [Get-Member](xref:Microsoft.PowerShell.Utility.Get-Member) -- [Select-Object](xref:Microsoft.PowerShell.Utility.Select-Object) +- [about_Objects][03] +- [about_Member-Access_Enumeration][01] +- [about_Methods][02] +- [Format-List][04] +- [Get-Member][05] +- [Select-Object][06] + + +[01]: about_Member-Access_Enumeration.md +[02]: about_Methods.md +[03]: about_Objects.md +[04]: xref:Microsoft.PowerShell.Utility.Format-List +[05]: xref:Microsoft.PowerShell.Utility.Get-Member +[06]: xref:Microsoft.PowerShell.Utility.Select-Object