Below you may find a simple way to pick up the folder size and subfolder sizes in PowerShell.
function Get-FolderSizeTree {
<#
.SYNOPSIS
Retrieves folder size information in a structured way.
.DESCRIPTION
Retrieves folder size information in a structured way.
.EXAMPLE
PS C:\> Get-FolderSizeTree .\sdwork\ | ft
ParentName Name SizeMB SizeGB
---------- ---- ------ ------
.\sdwork\ 36.19 0.04
C:\sdwork 29.58 0.03
C:\sdwork\binwin 1.33 0
C:\sdwork\bsddb 0.06 0
C:\sdwork\bsddb\test 0.09 0
C:\sdwork\compiler 0.15 0
C:\sdwork\distutils 0.34 0
C:\sdwork\distutils\command 0.18 0
C:\sdwork\email 0.13 0
C:\sdwork\email\test 0.1 0
#>
[CmdletBinding()]
param (
[parameter(mandatory=$true,ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true )]
[System.IO.DirectoryInfo[]]$Name,
[Switch]$ShowNonEmptyDirsOnly,
[Switch]$SortBySize
)
BEGIN{}
PROCESS{
foreach($name1 in $name){
$out2 = Get-ChildItem -Force -Recurse $Name1 -File |
Select-Object LastWriteTime, @{n='SizeMB'; e={[math]::round(($_.length / 1MB),3)}}, name, directory |
Group-Object directory |
Select-Object @{n='ParentName';e={$Null}}, Name,
@{n='SizeMB' ; e={[math]::Round((($_.group.sizeMB | Measure-Object -Sum).sum),3)}} ,
@{n='SizeGB' ; e={[math]::Round((($_.group.sizeMB | Measure-Object -Sum).sum/1024),3)}}
$out1 = "" | Select-Object @{n='ParentName';e={$Name1}},
@{n='Name';e={$Null}},
@{n='SizeMB';e={[math]::Round((($out2.SizeMB | Measure-Object -sum).sum),3)}},
@{n='SizeGB' ; e={[math]::Round((($out2.sizeMB | Measure-Object -Sum).sum/1024),3)}}
$Total = @()
$Total += $out1
$Total += $out2
IF($ShowNonEmptyDirsOnly){
IF($SortBySize){ $Total | ?{$_.sizeMB -gt 0} | Sort-Object -Property SizeMB }ELSE{ $Total | ?{$_.sizeMB -gt 0 } }
}ELSE{
IF($SortBySize){ $Total | Sort-Object -Property SizeMB }ELSE{ $Total }
}
}
}
END{}
}