Skip to content

Instantly share code, notes, and snippets.

@etcetra7n
Last active January 3, 2026 16:28
Show Gist options
  • Select an option

  • Save etcetra7n/8f4dd3aa00188a437315d667051b8031 to your computer and use it in GitHub Desktop.

Select an option

Save etcetra7n/8f4dd3aa00188a437315d667051b8031 to your computer and use it in GitHub Desktop.
Compare Sizes across files and folders powershell utility

LsSize Utility

Registry command key:

C:\scripts\singleinstance.exe "%1" "LsSizeLauncher.bat" "$files" --si-timeout 300
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
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\CompareSizes]
@="Compare sizes"
"Icon"="F:\\icons\\folder.ico"
[HKEY_CLASSES_ROOT\Directory\shell\CompareSizes\command]
@="C:\\scripts\\singleinstance.exe \"%1\" \"LsSizeLauncher.bat\" \"$files\" --si-timeout 300"
@echo off
powershell -NoProfile -ExecutionPolicy Bypass -File "C:\scripts\LsSize.ps1" %*
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment