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???
Asked
Active
Viewed 342 times
4

Sergey Salikov
- 53
- 3
1 Answers
5
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)
-
So why then `$a | Measure-Object -Property length -sum` gets an error "length property not found"? – Sergey Salikov Apr 21 '15 at 02:27
-
@Matt - Get-ChildItem may return one or more objects, but it's just a stream. If there's more than one object returned, and the result is assigned to a variable or object property, then it becomes an array when it's "at rest". The creation of the array is a function of pipeline or indirection operations, not the Get-ChildItem cmdlet. – mjolinor Apr 21 '15 at 02:38
-
You are correct I was not giving enough information in my comment. How many items being returned influences the output is more of what I was getting at. Thanks for the tip – Matt Apr 21 '15 at 02:39