Geeks With Blogs
Alex Hildyard

Be aware that this line:

gci -r "C:\temp\test"  | measure-object -property length -sum

will throw an error if it encounters a folder whose only contents is another (empty) folder; this is because measure-object tries in this case to measure an object which does not have a “length” property defined:

PS C:\Projects> Get-ChildItem -Recurse "C:\temp\test"  | measure-object -property length -sum

measure-object : The property "length" cannot be found in the input for any objects.

At line:1 char:42

+ ... dItem -Recurse "C:\temp\test"  | measure-object -property length -sum

+                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException

    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand

 

 

There are various ways to achieve the correct effect, based on the idea that objects returned by gci that are directories will have the PSIsContainer property set to $true. For example:

 

$root = "c:\Oracle"

 

$total_files = 0

$total_size = [int]0

 

[System.Collections.Stack]$stack = @()

$stack.Push($root)

 

while ($stack.Length -gt 0)

{

    $folder = $stack.Pop()

 

    gci $folder |% `

    {

        $item = $_

       

        if ($item.PSIsContainer)

        {

            $stack.Push($item.FullName)

        }

        else

        {

             $total_size += $_.Length

             $total_files ++

 

        }

     }

}

 

Write-Host "Total size: $([Math]::Round($total_size / 1Mb, 2)) Mb over $total_files files"

Posted on Wednesday, March 15, 2017 3:56 PM | Back to top


Comments on this post: Using Measure-Object to sum file sizes in Powershell

# <a href="http://changemysoftware.co/">change my software</a>
Requesting Gravatar...
download change my software 10 edition to install windows on Android.
Left by change on Mar 20, 2017 9:42 AM

# re: Using Measure-Object to sum file sizes in Powershell
Requesting Gravatar...
Good post..Zellmeonline
Left by Anu on Mar 23, 2017 4:02 AM

# re: Using Measure-Object to sum file sizes in Powershell
Requesting Gravatar...
Running down on battery ! dont worry extend it upto 50% with Greenify Apk Battery Saver
Left by Zeel on Jul 02, 2017 1:13 PM

# re: Using Measure-Object to sum file sizes in Powershell
Requesting Gravatar...
This could be an issue as many of the file systems include such empty folders inside folders. Therefore it’s a common form of error which could occur to people who don’t know the real reason of the error. Thanks for sharing the solutions for the issue. Shared Office Spaces
Left by Taylor Shaw on Jul 20, 2017 1:37 PM

# re: Using Measure-Object to sum file sizes in Powershell
Requesting Gravatar...
Thanks for the detailed explanation of such a difficult topic for a beginner, it's all very clearly narrated
restaurants near me
Left by maodzedun on Jul 28, 2017 11:35 AM

# re: Using Measure-Object to sum file sizes in Powershell
Requesting Gravatar...
I have been looking for an idea to measure the file sizes in the application. Powershell software is pretty easy to handle and there are so many preferences which most of the users are not aware of. Thanks for the details and keep sharing more of them. www.babyloncitytours.com
Left by lionel on Aug 18, 2017 9:27 AM

# re: Using Measure-Object to sum file sizes in Powershell
Requesting Gravatar...
Hey guys ! Follow this web link tank trouble online play and play this with your friends and other even without pay any thing for the game.You don't even download or register for the game also.Thank you so much.
Left by Max994 on Oct 11, 2017 11:43 AM

# re: Using Measure-Object to sum file sizes in Powershell
Requesting Gravatar...
The source code helps me to understand the program easily. But I think need more clarification for a better understanding of the topic. Hope you will give more details as soon as possible. Thank you so much for the share. wooden blocks
Left by hila on Dec 13, 2017 9:34 AM

# re: Using Measure-Object to sum file sizes in Powershell
Requesting Gravatar...
All the required codes should be placed in order to install correctly<The a href="https://bestbritishessays.com/papersowl-com-review/">papersowl is helping them to increase the quality.
Left by kaffi on Dec 27, 2017 5:10 PM

# re: Using Measure-Object to sum file sizes in Powershell
Requesting Gravatar...
All the required codes should be placed in order to install correctlyThe papersowl is helping them to increase the quality.
Left by kaffi on Dec 27, 2017 5:12 PM

# re: Using Measure-Object to sum file sizes in Powershell
Requesting Gravatar...
I am so impressed with this! Thanks for sharing this. Hotel in kottayam
Left by Alice on Jan 09, 2018 8:46 AM

Your comment:
 (will show your gravatar)


Copyright © Alex Hildyard | Powered by: GeeksWithBlogs.net