Search code examples
powershellfilesizeget-childitem

Adding the file sizes of Get-ChildItem listing


My goal is to figure out how much space all images on my network drives are taking up.

So my command to retrieve a list of all images is this:

Get-ChildItem -recurse -include *jpg,*bmp,*png \\server01\folder

Then I would like to just retrieve the file size (Length).

Get-ChildItem -recurse -include *jpg,*bmp,*png \\server01\folder | Select-Object -property Length

Now this outputs:

                                         Length
                                         ------
                                         85554
                                         54841
                                         87129
                                        843314  

I don't know why it is aligned to the right, but I want to grab each length and add them all together. I am lost and have tried every thing I know (which isn't much since I am new to PS), tried searching Google but couldn't find any relevant results.

Any help or alternative methods are appreciated!


Solution

  • Use the Measure-Object cmdlet:

    $files = Get-ChildItem -Recurse -Include *jpg,*bmp,*png \\server01\folder
    $totalSize = ($files | Measure-Object -Sum Length).Sum
    

    To get the size in GB divide the value by 1GB:

    $totalSize = ($files | Measure-Object -Sum Length).Sum / 1GB