-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Treating scalars implicitly as collections doesn't fully work with custom objects ([pscustomobject]) - lacks a .Count property #3671
Description
Treating scalars as if they were collections is a powerful feature that eliminated many bugs when it was introduced in PSv3: it means that even a single object returns meaningful values for .Count and .Length and allows indexing with [0].
Unfortunately, not all scalar objects behave this way, a notable exception being custom objects ([pscstomobject]), such as the instances created by Select-Object.
The problem is the absence of the .Count / .Length properties.
Similarly, using collection operators .Where() and .ForEach() also does not work.
By contrast, indexing custom objects with [0] does work.
I wonder if there are other cases where cmdlets return objects (other than [pscustomobject] instances) that behave the same way.
Steps to reproduce
$null.Count
(Get-Item /).Count
(Get-Item / | Select-Object Name).Count # !! produces no output
([pscustomobject] @{ foo = 'bar' }).Count # !! produces no outputExpected behavior
0
1
1
1Actual behavior
0
1^ 3rd and 4th command didn't produce output, because the implicitly and explicitly created [pscustomobject] instances have no .Count and .Length properties.
Environment data
PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Darwin Kernel Version 16.5.0: Fri Mar 3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64