我正在创建一个菜单,其中一个选项是报告指定文件夹的文件夹大小,并将其显示给用户。在我输入文件夹名称后
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发布于 2017-04-09 23:52:18
文件夹中没有文件,因此您只能得到没有length-property的DirectoryInfo-objects。您可以使用以下命令过滤仅限文件的文件来避免此问题:
(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)https://stackoverflow.com/questions/43308663
复制相似问题