21

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!

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user2517266
  • 335
  • 1
  • 2
  • 6

2 Answers2

38

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • For any who'd like a one-liner version: `(Get-ChildItem -Path \\server01\folder -Recurse -Include *jpg,*bmp,*png | Measure-Object -Sum Length).Sum` – David Metcalfe Oct 06 '22 at 16:45
13

You could do this as a one liner with something like this.

Get-Childitem -path  "C:\Program Files\Internet Explorer" | Select-Object 
Name, @{Name="KBytes";Expression={ "{0:N0}" -f ($_.Length / 1KB) }}

Name          KBytes
----          ------
en-US         0
images        0
SIGNUP        0
ExtExport.exe 52
hmmapi.dll    53
iediagcmd.exe 500
ieinstal.exe  490
ielowutil.exe 219
IEShims.dll   398
iexplore.exe  805
sqmapi.dll    49
William Foster
  • 139
  • 1
  • 3