Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 918
user2517266 Avatar asked Jul 16 '13 17:07

user2517266


People also ask

How do I get the size of a directory in PowerShell?

You can use the Get-ChildItem ( gci alias) and Measure-Object ( measure alias) cmdlets to get the sizes of files and folders (including subfolders) in PowerShell.

What does Get-ChildItem do in PowerShell?

Description. The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.

How do I check the size of a file in PowerShell?

Use the Length Property to Get File Size in KB Using PowerShell. The file objects have the Length property in PowerShell. It represents the size of the file in bytes.

How do I get a list of files in PowerShell?

PowerShell utilizes the “Get-ChildItem” command for listing files of a directory. The “dir” in the Windows command prompt and “Get-ChildItem” in PowerShell perform the same function.


1 Answers

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
like image 114
Ansgar Wiechers Avatar answered Sep 30 '22 02:09

Ansgar Wiechers