Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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
like image 999
erik121 Avatar asked Apr 09 '17 15:04

erik121


People also ask

How do I find the length of an object in PowerShell?

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.

How do I use .count in PowerShell?

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.


1 Answers

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)
like image 191
Frode F. Avatar answered Sep 20 '22 12:09

Frode F.