Stole from a comment from reddit User u/surfingoldelephant
Created
August 31, 2023 15:24
-
-
Save webtroter/6058b175d35adc3c9fcc7cd76f4cf4ff to your computer and use it in GitHub Desktop.
Convert-FileSize
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
function Convert-FileSize { | |
[CmdletBinding()] | |
[OutputType([string])] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[int64] $FileSize | |
) | |
switch ($FileSize) { | |
{ $_ -lt 1KB } { '{0:#,##0} B' -f $FileSize; Break } | |
{ $_ -lt 1MB } { '{0:#,##0.###} KB' -f ($FileSize / 1KB); Break } | |
{ $_ -lt 1GB } { '{0:#,##0.###} MB' -f ($FileSize / 1MB); Break } | |
{ $_ -lt 1TB } { '{0:#,##0.###} GB' -f ($FileSize / 1GB); Break } | |
{ $_ -lt 1PB } { '{0:#,##0.###} TB' -f ($FileSize / 1TB); Break } | |
default { '{0:#,##0.###} PB' -f ($FileSize / 1PB) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment