You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PSCustomObject still has properties like Count and ForEach, even in strict mode #15526
The error text below when trying to access the Count property.
The property 'Count' cannot be found on this object. Verify that the property exists.
Actual behavior
The Count property returns the value of 1.
1
Environment data
Name Value
---- -----
PSVersion 7.1.3
PSEdition Core
GitCommitId 7.1.3
OS Microsoft Windows 10.0.19042
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
More detail
Basically, one of the features of the latest version of strict mode is that trying to access non-existent properties on an object results in an exception. This includes properties like Count, which are available on all objects outside of strict mode.
However, after #3671 was resolved, PSCustomObject no longer throws when you try to access Count in strict mode. The same is true for Length. This differs from the behavior of other objects such as numbers, strings, and process objects, which throw when Count is accessed.
If we want to follow #2798, then potentially the other objects like numbers should have their behavior changed. But as it stands, it seems like PSCustomObject is special in that it doesn't match the behavior of other types in strict mode.
Sorry if I didn't clarify enough in the issue description. In your example, I wouldn't expect any of those to throw in strict mode either. PSCustomObject doesn't throw, and that's the subject of this issue. Strings won't throw when accessing Length, since that's a valid property on strings. They will throw when accessing Count, though. (gps).Count doesn't throw, since gps returns an array, and arrays have a Count property.
My reasoning behind creating the issue is that most objects that don't have Count (or Length) properties throw in strict mode when you try to access those properties. PSCustomObject doesn't throw, however, which seems like inconsistent behavior.
Does that make sense?
PS>Set-StrictMode-Version Latest
PS>'aaa'.Count
PropertyNotFoundException: The property 'Count' cannot be found on this object. Verify that the property exists.
PS>(gps)[0].Count
PropertyNotFoundException: The property 'Count' cannot be found on this object. Verify that the property exists.
PS>(4).Count
PropertyNotFoundException: The property 'Count' cannot be found on this object. Verify that the property exists.
PS>
(gps).Count doesn't throw, since gps returns an array, and arrays have a Count property.
I don't think that is true, Array does not have a Count property, they just have Length so this is another edge case where Count is present. I don't really have any strong feelings either way though, Strict Mode is mostly a curiosity for me and not something I've used successfully in the past.
Count tends to persist on most PS objects because it is an automatic property. If you check the Count property of any non-array it will generally give you the result 1, except if the value you're checking is $null, when it may be 0.
This is so that users don't have to be hypervigilant over whether a given pipeline result is one or multiple items, and can simply check Count.
Not sure how consistently it works in strict mode, though.
Reacted by Keith Hill, Matthew Gray and Max Kozlov
Engine-supplied members such as .Count, .Length, .psobject, ForEach(), .... now have an official name: intrinsic members.
It makes much more sense to make them consistently available irrespective of strict mode.
This is already true for most of them, but inexplicably not for .Count and .Length (in general), despite their tremendous usefulness in unified handling of scalars and collections, as mentioned by Rain Sallow (/u/ta11ow) (@vexx32).
In short: Fix #2798, and everyone will live happily ever after.
Set-StrictMode isn't heavily used anyway, but at least for me it was #2798 that prevented me from ever using Set-StrictMode -Version 2 or higher.
Here's a quick demonstration of the inconsistency:
# .psobject, .ForEach(), scalar indexing already work in all strict modes, .Count does not.
PS>& { Set-StrictMode-version Latest; ('foo').psobject; ('foo').ForEach({"[$_]"}); (42)[0]; ('foo').Count }
BaseObject : foo
Members : {char Chars(int index) {get;}, int Length {get;}, Remove, Replace…}
Properties : {int Length {get;}}
Methods : {Remove, Replace, Split, Substring…}
ImmediateBaseObject : foo
TypeNames : {System.String, System.Object}
[foo]
42
PropertyNotFoundException: The property 'Count' cannot be found on this object. Verify that the property exists.
Reacted by Matthew Gray and Patrick Meinecke
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
Activity
237dmitry commentedon Jun 5, 2021
They do not throw:
binyomen commentedon Jun 5, 2021
Sorry if I didn't clarify enough in the issue description. In your example, I wouldn't expect any of those to throw in strict mode either.
PSCustomObjectdoesn't throw, and that's the subject of this issue. Strings won't throw when accessingLength, since that's a valid property on strings. They will throw when accessingCount, though.(gps).Countdoesn't throw, sincegpsreturns an array, and arrays have aCountproperty.My reasoning behind creating the issue is that most objects that don't have
Count(orLength) properties throw in strict mode when you try to access those properties.PSCustomObjectdoesn't throw, however, which seems like inconsistent behavior.Does that make sense?
jborean93 commentedon Jun 5, 2021
I don't think that is true,
Arraydoes not have aCountproperty, they just haveLengthso this is another edge case whereCountis present. I don't really have any strong feelings either way though, Strict Mode is mostly a curiosity for me and not something I've used successfully in the past.binyomen commentedon Jun 5, 2021
From using
Get-Member, it seems like arrays have both aCountand aLengthproperty, although I might be missing something:However,
PSCustomObjectdoesn't have aCountproperty:Neither do strings or numbers (
Int32in this case):237dmitry commentedon Jun 5, 2021
I think this was left for backward compatibility with Windows Powershell, where
Countwas an alias forLengthand very many scripts use both of them.jborean93 commentedon Jun 5, 2021
It’s not documented in the Array type. While it appears as an adapted property it’s still not part of the underlying type AFAIK.
binyomen commentedon Jun 5, 2021
It looks like it is documented there. Under "Explicit Interface Implementations" the page lists
ICollection.Count.vexx32 commentedon Jun 6, 2021
Counttends to persist on most PS objects because it is an automatic property. If you check theCountproperty of any non-array it will generally give you the result1, except if the value you're checking is$null, when it may be0.This is so that users don't have to be hypervigilant over whether a given pipeline result is one or multiple items, and can simply check
Count.Not sure how consistently it works in strict mode, though.
mklement0 commentedon Jul 20, 2021
Engine-supplied members such as
.Count,.Length,.psobject,ForEach(), .... now have an official name: intrinsic members.It makes much more sense to make them consistently available irrespective of strict mode.
This is already true for most of them, but inexplicably not for
.Countand.Length(in general), despite their tremendous usefulness in unified handling of scalars and collections, as mentioned by Rain Sallow (/u/ta11ow) (@vexx32).In short: Fix #2798, and everyone will live happily ever after.
Set-StrictModeisn't heavily used anyway, but at least for me it was #2798 that prevented me from ever usingSet-StrictMode -Version 2or higher.Here's a quick demonstration of the inconsistency:
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.