Automatic Unrolling Can Fail

by Apr 1, 2016

Beginning with PowerShell 3.0, there is a new feature called “Automatic Unrolling”: when you specify a property or method on an array, then the property or method is called on all elements of that array:

(Get-Process).Name

Unless you specify a property or method that is also present in the array itself. This is why automatic unrolling can fail in some cases:

(Get-ChildItem -Path c:\windows -File).Length


(Get-ChildItem -Path c:\windows -File) | Select-Object -ExpandProperty Length

The first call returns the array length, not the file length of the array elements. To access the array elements safely, use Select-Object with the –ExpandProperty parameter.

Twitter This Tip! ReTweet this Tip!