Last active
December 1, 2021 20:42
-
-
Save msftrncs/e2937c64396868efbf731084f9b89d7b to your computer and use it in GitHub Desktop.
Format-Bytes: My Version
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
filter Format-Bytes { | |
if ($_ -is [ValueType]) { | |
$x = if (0 -ne $_) { | |
[Math]::Min([int][Math]::Truncate([Math]::Log([Math]::Abs($_), 1kb)), 8) | |
} else { | |
0 | |
} | |
if ($x -eq 0) { | |
"$_ B" | |
} else { | |
"{0:N2} $('KMGTPEZY'[$x - 1])B" -f ($_ / [Math]::Pow(1kb, $x)) | |
} | |
} else { | |
$_ # return the input because it could not be processed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my take on the many variations of
Format-Bytes
function. I have applied it as afilter
, which means it works exclusively with pipelines.If
NULL
is input,NULL
will be returned.