|
param ( |
|
[Parameter(Mandatory = $true, ValueFromRemainingArguments = $true)] |
|
[string[]]$Paths |
|
) |
|
|
|
function Get-SizeReadable { |
|
param ([Int64]$bytes) |
|
$suffixes = "B","KB","MB","GB","TB","PB" |
|
$i = 0 |
|
while ($bytes -ge 1024 -and $i -lt $suffixes.Length - 1) { |
|
$bytes = [math]::Round($bytes / 1024, 2) |
|
$i++ |
|
} |
|
return "$bytes $($suffixes[$i])" |
|
} |
|
|
|
function Format-Name { |
|
param ( |
|
[string]$Name, |
|
[int]$MaxLength = 32 |
|
) |
|
if ($Name.Length -le $MaxLength) { |
|
return $Name |
|
} |
|
# Keep start + end, truncate middle |
|
$keepStart = [math]::Floor(($MaxLength - 3) / 2) |
|
$keepEnd = $MaxLength - 3 - $keepStart |
|
return ($Name.Substring(0, $keepStart) + "..." + $Name.Substring($Name.Length - $keepEnd)) |
|
} |
|
|
|
$dirs = @() |
|
$files = @() |
|
$allResults = @() |
|
|
|
if ($Paths.Count -eq 1) { |
|
$Paths = @(Get-ChildItem -LiteralPath $Paths | Select-Object -ExpandProperty FullName) |
|
} |
|
|
|
foreach ($path in $Paths) { |
|
$item = Get-Item $path |
|
if ($item.PSIsContainer) { |
|
$dirs += $path |
|
} else { |
|
$files += $path |
|
} |
|
} |
|
|
|
# ---------- HEADER ---------- |
|
Write-Host ("{0,-36} {1,-15} {2}" -f "Name", "Count", "Size") -ForegroundColor Green |
|
Write-Host ("{0,-36} {1,-15} {2}" -f "----", "-----", "----") -ForegroundColor Green |
|
|
|
# ---------- FOLDERS ---------- |
|
foreach ($path in $dirs) { |
|
$stats = Get-ChildItem -Recurse -File -Path $path -Force | Measure-Object Length -Sum |
|
$displayName = Format-Name (Split-Path -Path $path -Leaf) |
|
|
|
$allResults += [PSCustomObject]@{ |
|
Name = $displayName |
|
Count = "$($stats.Count) files" |
|
Size = $stats.Sum |
|
IsDir = $true |
|
} |
|
|
|
$sizeFormatted = Get-SizeReadable $stats.Sum |
|
$color = if ($stats.Sum -ge 1073741824) { "Red" } else { "Cyan" } |
|
|
|
Write-Host ("{0,-36} {1,-15} {2}" -f $displayName, "$($stats.Count) files", $sizeFormatted) -ForegroundColor $color |
|
} |
|
|
|
# ---------- FILES ---------- |
|
foreach ($path in $files) { |
|
$item = Get-Item $path -Force |
|
$displayName = Format-Name (Split-Path -Path $path -Leaf) |
|
|
|
$allResults += [PSCustomObject]@{ |
|
Name = $displayName |
|
Count = "" |
|
Size = $item.Length |
|
IsDir = $false |
|
} |
|
|
|
$sizeFormatted = Get-SizeReadable $item.Length |
|
$color = if ($item.Length -ge 1073741824) { "Red" } else { "Yellow" } |
|
|
|
Write-Host ("{0,-36} {1,-15} {2}" -f $displayName, "", $sizeFormatted) -ForegroundColor $color |
|
} |
|
|
|
# ---------- SORTED TABLE (DIRS + FILES) ---------- |
|
Write-Host |
|
Write-Host "Items sorted by size (descending)" -ForegroundColor Green |
|
Write-Host ("{0,-36} {1,-15} {2}" -f "Name", "Count", "Size") -ForegroundColor Green |
|
Write-Host ("{0,-36} {1,-15} {2}" -f "----", "-----", "----") -ForegroundColor Green |
|
|
|
$allResults | |
|
Sort-Object Size -Descending | |
|
ForEach-Object { |
|
$sizeFormatted = Get-SizeReadable $_.Size |
|
$color = if ($_.Size -ge 1073741824) { |
|
"Red" |
|
} elseif ($_.IsDir) { |
|
"Cyan" |
|
} else { |
|
"Yellow" |
|
} |
|
|
|
Write-Host ("{0,-36} {1,-15} {2}" -f (Format-Name $_.Name), $_.Count, $sizeFormatted) -ForegroundColor $color |
|
} |
|
|
|
Write-Host |