`Set-StrictMode 2`: False positive for references to the `Count` property
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Set-StrictMode -Version Latest
$TempFile = New-TemporaryFile
$TempPath = Join-Path $Env:Temp ('Folder_' + [Guid]::NewGuid())
$TempFolder = New-Item -ItemType Directory -Path $TempPath
Copy-Item -Path $TempFile -Destination $TempFolder
$TempFolder |Should -Exist
(Get-ChildItem $TempFolder).Count |Should -Be 1
Expected behavior
No Error
Actual behavior
PropertyNotFoundException: The property ‘Count’ cannot be found on this object. Verify that the property exists.
Error details
With Set-StrictMode -Off
the (Get-ChildItem $TempFolder).Count
return 1
which shows that the property does exist
Environment data
Name Value
---- -----
PSVersion 7.2.0
PSEdition Core
GitCommitId 7.2.0
OS Microsoft Windows 10.0.22000
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (4 by maintainers)
Top Results From Across the Web
Objects with no '.Count' Property - use of @() (array ...
Count property unexpectedly fails when Set-StrictMode -Version 2 or higher is in effect, which is a long-standing bug reported in GitHub ...
Read more >Set-StrictMode (Microsoft.PowerShell.Core)
Uninitialized variables are assumed to have a value of 0 (zero) or $Null , depending on type; References to non-existent properties return $Null;...
Read more >The Poster of the Plethora of PowerShell Pitfalls - Simple Talk
Arithmetic operators produce unexpected results with arrays (precedence issue). Issue: Simple arithmetic operations with an array seem wrong.
Read more >Strict mode @ strict_mode @ StarRocks Docs
Strict mode is an optional property that you can configure for data loads. It affects the loading behavior and the final loaded data....
Read more >Class PgxConfigBuilder
Examples: A value of 0.0 means graphs get freed as soon as their reference count becomes zero. That is, all sessions which loaded...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That makes sense to me. If strict mode is intended to by design restrict usage of otherwise ubiquitous properties provided by the engine, that should be well documented. Given that it’s not (as far as I’m aware?), I would be inclined to assume it’s more of an accident of implementation and we should see if it’s feasible to rectify the issue.
Note that this problematic behavior was first reported in
#15526 also discusses the inconsistency, but argues for the opposite resolution; to borrow from my comment there:
Engine-supplied members such as
.Count
,.Length
, scalar[...]
,.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
([pscustomobject]
being the only exception), despite their tremendous usefulness in unified handling of scalars and collections.Simplest repro:
@iRon7, your
[string[]] $StringArray
parameter example can be reduced to$null.Count
, because unbound reference-type parameters (without default values) are$null
.