NULL values have a Count property

by Dec 4, 2012

In a previous tip we explained that in PowerShell 3.0, every object has a Count property now. This even includes null values:

PS> $null.Count

This is so that when a command returns "nothing", you still can find out how many items you received by querying Count.

In PowerShell 3.0, you can always use Count to find out whether you received some result, and if so, how many results you got. Except if you use Strict-Mode. Then, the magic Count is removed.

This redesign might also be in the context of another change: folder objects no longer have a truly empty Length property. In PowerShell 2.0, you were able to list folders this way:

Get-ChildItem $env:windir | Where-Object { $_.Length -eq $null }

This no longer works in PowerShell 3.0. You would have to resort to another suitable property (or use the new PowerShell 3.0 parameters in Get-ChildItem):

PS> Get-ChildItem $env:windir | Where-Object { $_.PSIsContainer }
PS> Get-ChildItem $env:windir -Directory

Twitter This Tip! ReTweet this Tip!