POWERSHELL Version 1.0.0
Folder Size Analyzer
Get the files and sizes in a tree like view.
Source code
SHA-256:02d991b8fe4af46691df10cf0a55388179031c8351af890231b06156572eb7a6
# ScriptSnatch.com
# Saved Wi-Fi Network Viewer
# No external dependencies
$ErrorActionPreference = "Stop"
Write-Host ""
# ScriptSnatch.com
# Folder Size Tree Analyzer
# No external dependencies
$ErrorActionPreference = "Stop"
function Format-Size {
param([long]$Bytes)
if ($Bytes -ge 1GB) {
return "{0:N2} GB" -f ($Bytes / 1GB)
}
elseif ($Bytes -ge 1MB) {
return "{0:N2} MB" -f ($Bytes / 1MB)
}
elseif ($Bytes -ge 1KB) {
return "{0:N2} KB" -f ($Bytes / 1KB)
}
return "$Bytes B"
}
function Get-FolderTree {
param([string]$Path)
$node = [PSCustomObject]@{
Name = Split-Path $Path -Leaf
Path = $Path
Size = [long]0
Children = @()
}
# Avoid following directory junctions / symbolic links
try {
$items = @(Get-ChildItem -LiteralPath $Path -Force -ErrorAction Stop)
}
catch {
Write-Warning "Cannot access: $Path"
return $node
}
foreach ($item in $items) {
if ($item.PSIsContainer) {
if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) {
continue
}
$child = Get-FolderTree -Path $item.FullName
$node.Children += $child
$node.Size += $child.Size
}
else {
$node.Size += $item.Length
}
}
return $node
}
function Show-FolderTree {
param(
$Node,
[double]$TotalSize,
[string]$Prefix = "",
[bool]$IsLast = $true,
[bool]$IsRoot = $false
)
if ($TotalSize -gt 0) {
$percent = ($Node.Size / $TotalSize) * 100
}
else {
$percent = 0
}
$sizeText = Format-Size $Node.Size
if ($IsRoot) {
$name = $Node.Path
}
else {
$branch = if ($IsLast) { "\-- " } else { "|-- " }
$name = "$Prefix$branch$($Node.Name)"
}
Write-Host ("{0,-55} {1,12} {2,7:N1}%" -f `
$name, $sizeText, $percent)
$children = @($Node.Children | Sort-Object Size -Descending)
for ($i = 0; $i -lt $children.Count; $i++) {
$lastChild = ($i -eq ($children.Count - 1))
if ($IsRoot) {
$nextPrefix = ""
}
elseif ($IsLast) {
$nextPrefix = "$Prefix "
}
else {
$nextPrefix = "$Prefix| "
}
Show-FolderTree `
-Node $children[$i] `
-TotalSize $TotalSize `
-Prefix $nextPrefix `
-IsLast $lastChild
}
}
Clear-Host
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " FOLDER SIZE TREE ANALYZER" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
$rootPath = Read-Host "Enter starting folder path"
try {
$rootPath = (Resolve-Path -LiteralPath $rootPath -ErrorAction Stop).Path
if (-not (Test-Path -LiteralPath $rootPath -PathType Container)) {
throw "The path is not a folder."
}
Write-Host ""
Write-Host "Scanning folders. Please wait..." -ForegroundColor Yellow
Write-Host ""
$tree = Get-FolderTree -Path $rootPath
$totalSize = [double]$tree.Size
Write-Host "ROOT: $rootPath" -ForegroundColor Cyan
Write-Host "TOTAL SIZE: $(Format-Size $tree.Size)"
Write-Host ""
Write-Host ("{0,-55} {1,12} {2,8}" -f `
"PATH", "SIZE", "USED")
Write-Host ("-" * 80)
Show-FolderTree `
-Node $tree `
-TotalSize $totalSize `
-IsRoot $true
Write-Host ""
Write-Host "Scan completed." -ForegroundColor Green
}
catch {
Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red
}
Community reviews
No published reviews yet.
Leave a review
Reviews are moderated. Your email is not displayed publicly.