Sergey Salikov
Sergey Salikov

Reputation: 53

Length property of DirectoryInfo object

There exists empty directory "New Folder" inside K:\test
>$a = gci K:\test
Directory shouldn't have the Length property. Let's check:
[bool]($a.PSobject.Properties.Name -match "Length") False Yes, it doesn't have.
But:
>$a.Length 1 What does that mean???

Upvotes: 4

Views: 375

Answers (1)

mjolinor
mjolinor

Reputation: 68301

This is a synthetic property that was added in V3, to prevent errors in scripts where an expression that would normally be expected to return an array may return a scalar, and cause array operations to fail.

You can now use Count or Length on any object, even if it didn’t have the property. If the object didn’t have a Count or Length property, it will will return 1 (or 0 for $null). Objects that have Count or Length properties will continue to work as they always have.

PS> $a = 42
PS> $a.Count
1

source (archived link)

Upvotes: 5

Related Questions