Skip to content

Instantly share code, notes, and snippets.

@webtroter
Created August 31, 2023 15:24
Show Gist options
  • Save webtroter/6058b175d35adc3c9fcc7cd76f4cf4ff to your computer and use it in GitHub Desktop.
Save webtroter/6058b175d35adc3c9fcc7cd76f4cf4ff to your computer and use it in GitHub Desktop.
Convert-FileSize
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