Automatically formats a byte count as Bytes, KB, MB, GB or TB, with selectable decimal precision.

PowerShell
Edit|Remove
function to-kmg {
param ($bytes,$precision='0')
foreach ($i in ("Bytes","KB","MB","GB","TB")) {
if (($bytes -lt 1000) -or ($i -eq "TB")){
$bytes = ($bytes).tostring("F0" + "$precision")
return $bytes + " $i"
}
else {$bytes /= 1KB}
}
}