Kaseya Community

Powershell experts needed :)

This question is not answered

Hey guys, lookin to expand on a PS script that i'm piecing together. Found a nifty little function that can formulate the sizes of a directory for me which is awesome. Right now I'm just focused on reporting on one directory. End result I would like basically like the sum of the size of all the directories I've looked at.

Here's what I've got so far:

$colItems = Get-ChildItem $startFolder, $startFolder2, $startFolder3 | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
foreach ($i in $colItems)
{
$subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
$i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1KB) + "KB"}

!!startFolder3 is currently empty.!!

Results: 

C:\Users\ceadmin\Appdata\Local\Temp -- 0.00KB
C:\Users\ceadmin\AppData\Roaming\Microsoft\Windows\Recent -- 86.83KB
C:\Users\naltobelli\Appdata\Local\Temp -- 0.00KB
C:\Users\naltobelli\AppData\Roaming\Microsoft\Windows\Recent -- 1,076.59KB
C:\Users\rbibeau\Appdata\Local\Temp -- 0.00KB
C:\Users\rbibeau\AppData\Roaming\Microsoft\Windows\Recent -- 22.47KB
C:\Users\rshaw\Appdata\Local\Temp -- 9,109.96KB
C:\Users\rshaw\AppData\Roaming\Microsoft\Windows\Recent -- 1,188.23KB

So how do I get it to report a total of the sizes? Keep adding it to a variable somehow?

All Replies
  • Hey guys! Solved my own question.

    --------------------------------------------------------------------------------------------------------------------

    $total = 0

    $colItems = Get-ChildItem $startFolder, $startFolder2, $startFolder3 | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object

    foreach ($i in $colItems)

    {

       $subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum

       $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1KB) + "KB"

       $total = $total + $subFolderItems.sum / 1KB

    }

    "Total: " + "{0:N2}" -f ($total) + "KB"  

    --------------------------------------------------------------------------------------------------------------------

    If you've got any other ways, I'd love to hear it!

  • Another option is here in case you are interested in speed (claims to be 120% faster)

    www.powershelladmin.com/.../Get_Folder_Size_with_PowerShell,_Blazingly_Fast

    But I personally prefer simplicity and the way you are doing it is the currently recommended way.