简体   繁体   中英

Measure-Object : The property “length” cannot be found in the input for any objects

I'm creating a menu and one of the options is to report on folder size for a specified folder and display it back to the user. After I put in the folder name

cls
$Path = Read-Host -Prompt 'Please enter the folder name: '

$FolderItems = (Get-ChildItem $Path -recurse | Measure-Object -property length -sum)      

$FolderSize = "{0:N2}" -f ($FolderItems.sum / 1MB) + " MB"

I get the following error:

Measure-Object : The property "length" cannot be found in the input for any objects.
At C:\Users\Erik\Desktop\powershell script.ps1:53 char:48
+ ... (Get-ChildItem $Path -recurse | Measure-Object -property length -sum)
+                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands. 
   MeasureObjectCommand

There are no files in the folders, so you only get DirectoryInfo -objects which doesn't have a length -property. You can avoid this by filtering for files-only using:

(Get-ChildItem $Path -Recurse | Where-Object { -not $_.PSIsContainer } | Measure-Object -property length -sum) 

Or for PS 3.0+

(Get-ChildItem $Path -Recurse -File | Measure-Object -property length -sum)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM