How does the Count property work in Powershell?

Starting in PowerShell V3, the properties Count and Length received very special treatment not related to extended type data (also known as ETS or extended type system).

If the instance has a Count/Length property, then everything continues to work like it did in V1/V2 – the value from the instance is returned.

If the instance does not have a Count/Length property, starting in V3, instead of that being an error, you’ll get back 1. And if the instance is $null, you’ll get back 0. If you have turned on strict mode, you’ll get an error like in V2.

I’ll admit this is a bit strange, but it solves a common problem when a cmdlet returns 0, 1, or multiple objects.

Often, you’ll iterate through those results via the pipeline or with a foreach statement. For example:

dir nosuchfile* | % { $_ }
foreach ($item in dir nosuchfile*) { $_ }

In the foreach case above, V2 would actually enter the loop if the command didn’t return any values. That was changed in V3 to better match peoples expectations, but that also meant that:

foreach ($item in $null) { $_ }

also never enters the loop.

The for statement is another way to iterate through results, e.g.

$results = dir nosuchfile*
for ($i = 0; $i -lt $results.Count; $i++) { $results[$i] }

In this example, it doesn’t matter how many objects are in $result, the loop works just fine. Note that in V1/V2, you would have written:

$results = @(dir nosuchfile*)

This ensures $results is an array, but this syntax is ugly and many folks would forget it, hence the change to support Count/Length in a slightly more forgiving way.

Leave a Comment