Why Directories Have a Size of 1

by Jul 1, 2014

Occasionally, you may notice that folders have a length of 1 byte. This was introduced in PowerShell 3.0. In PowerShell 2.0, they did not report back any length.

$folder = Get-Item c:\Windows
$folder.Length

This artifact is caused by yet another feature. Any object that has neither a property "Count" or "Length" will get one, with the preset value of 1.

PS> $host.length
1
PS> $host.Count
1
PS> (Get-Date).Length
1

PS> (Get-Date).Count
1
PS> $ConfirmPreference.Length
1
PS> $ConfirmPreference.Count
1

In the past, typically only arrays returned a length (or count). So when a cmdlet or function returned just one result (or nothing), then this result is not wrapped into an array, and trying to find out the number of results led to an error.

With this new "feature", if a command returns just one object, the added "Count" property will always return "1", reporting that there is exactly one element returned.

Twitter This Tip! ReTweet this Tip!