Measure-Object:在任何对象的输入中都找不到属性“length”

时间:2017-04-09 15:40:18

标签: powershell

我正在创建一个菜单,其中一个选项是报告指定文件夹的文件夹大小并将其显示给用户。我输入文件夹名称

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"

我收到以下错误:

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

1 个答案:

答案 0 :(得分:6)

文件夹中没有文件,因此您只能获得DirectoryInfo - 没有length - 属性的对象。您可以通过使用以下方式过滤文件来避免这种情况:

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

或PS 3.0 +

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