Last active
May 21, 2026 14:29
-
-
Save FredoVelcro/4b2cc8c08a5d02d6417e208c86090cae to your computer and use it in GitHub Desktop.
PowerShell list folders sizes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # usage: .\folders_usage.ps1 'C:\Program Files (x86)\' -Tree 2 | |
| param( | |
| [Parameter(Mandatory=$true, Position=0)] | |
| [string]$Folder, | |
| [int]$Tree = 0 | |
| ) | |
| if (-not (Test-Path $Folder)) { | |
| Write-Error "Folder '$Folder' does not exist." | |
| exit | |
| } | |
| # Cache folder sizes | |
| $FolderSizes = @{} | |
| function Get-FolderSize { | |
| param([string]$Path) | |
| if ($FolderSizes.ContainsKey($Path)) { | |
| return $FolderSizes[$Path] | |
| } | |
| $size = (Get-ChildItem $Path -Recurse -File -ErrorAction SilentlyContinue | | |
| Measure-Object Length -Sum).Sum | |
| $FolderSizes[$Path] = $size | |
| return $size | |
| } | |
| # Format size as GB string, but return raw text (no padding) | |
| function Format-Size { | |
| param([long]$Bytes) | |
| $gb = [math]::Round($Bytes / 1GB, 2) | |
| return ("{0:0.##} GB" -f $gb) | |
| } | |
| # Width of the left column (tree + folder name) | |
| $LeftWidth = 60 | |
| # Width of the size text inside brackets | |
| $SizeWidth = 10 # adjust if needed | |
| function Get-Tree { | |
| param( | |
| [string]$Path, | |
| [int]$Level, | |
| [int]$MaxLevel, | |
| [string]$Prefix = "" | |
| ) | |
| if ($Level -gt $MaxLevel) { return } | |
| $dirs = Get-ChildItem $Path -Directory -ErrorAction SilentlyContinue | |
| for ($i = 0; $i -lt $dirs.Count; $i++) { | |
| $isLast = ($i -eq $dirs.Count - 1) | |
| $connector = if ($isLast) { "└── " } else { "├── " } | |
| $left = "$Prefix$connector$($dirs[$i].Name)" | |
| $size = Get-FolderSize $dirs[$i].FullName | |
| $sizeText = Format-Size $size | |
| # Right-align inside brackets | |
| $sizeAligned = ("{0,$SizeWidth}" -f $sizeText) | |
| # Final line | |
| $line = ("{0,-$LeftWidth} [{1}]" -f $left, $sizeAligned) | |
| Write-Output $line | |
| $newPrefix = if ($isLast) { "$Prefix " } else { "$Prefix│ " } | |
| Get-Tree -Path $dirs[$i].FullName -Level ($Level + 1) -MaxLevel $MaxLevel -Prefix $newPrefix | |
| } | |
| } | |
| # Warm cache (no output) | |
| Get-ChildItem $Folder -Directory | ForEach-Object { | |
| Get-FolderSize $_.FullName | Out-Null | |
| } | |
| # Tree mode | |
| if ($Tree -gt 0) { | |
| Write-Host "" | |
| Write-Host "Directory tree (max depth: $Tree)" -ForegroundColor Cyan | |
| $rootSize = Get-FolderSize $Folder | |
| $rootText = Format-Size $rootSize | |
| $rootAligned = ("{0,$SizeWidth}" -f $rootText) | |
| Write-Host ("{0,-$LeftWidth} [{1}]" -f $Folder, $rootAligned) | |
| Get-Tree -Path $Folder -Level 1 -MaxLevel $Tree | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment