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
You can use Measure-Object to count objects or count objects with a specified Property. You can also use Measure-Object to calculate the Minimum, Maximum, Sum, StandardDeviation and Average of numeric values. For String objects, you can also use Measure-Object to count the number of lines, words, and characters.
Use the Count Method in PowerShell to Count Objects Then, add a period ( . ), followed by the count. For example, we wanted to know how many files were in a folder. The initial step to counting files in a folder is to use the Get-ChildItem cmdlet to return the files.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With