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 output
Expected behavior
Actual behavior
^ 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
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
.Countand.Lengthand allows indexing with[0].Unfortunately, not all scalar objects behave this way, a notable exception being custom objects (
[pscstomobject]), such as the instances created bySelect-Object.The problem is the absence of the
.Count/.Lengthproperties.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
Expected behavior
Actual behavior
^ 3rd and 4th command didn't produce output, because the implicitly and explicitly created
[pscustomobject]instances have no.Countand.Lengthproperties.Environment data