Skip to content

Instantly share code, notes, and snippets.

@realslacker
Created July 19, 2018 01:44
Show Gist options
  • Save realslacker/b796bc16a6547e2c626eb7063b4c08d2 to your computer and use it in GitHub Desktop.
Save realslacker/b796bc16a6547e2c626eb7063b4c08d2 to your computer and use it in GitHub Desktop.
<#
.SUMMARY
Converts bytes values into human readable forms.
.DESCRIPTION
Converts bytes values into human readable forms.
.PARAMETER Bytes
Input in Bytes
.PARAMETER KB
Input in Kilobytes
.PARAMETER MB
Input in Megabytes
.PARAMETER GB
Input in Gigabytes
.PARAMETER TB
Input in Terabytes
.PARAMETER PB
Input in Petabytes
#>
[CmdletBinding(DefaultParameterSetName='Bytes')]
param(
[Parameter(Mandatory, ParameterSetName='Bytes')]
[int64]
$Bytes,
[Parameter(Mandatory, ParameterSetName='KB')]
[int64]
$KB,
[Parameter(Mandatory, ParameterSetName='MB')]
[int64]
$MB,
[Parameter(Mandatory, ParameterSetName='GB')]
[int64]
$GB,
[Parameter(Mandatory, ParameterSetName='TB')]
[int64]
$TB,
[Parameter(Mandatory, ParameterSetName='PB')]
[int64]
$PB
)
switch ( $PSCmdlet.ParameterSetName ) {
'KB' { $Bytes = $KB * 1024 }
'MB' { $Bytes = $MB * 1048576 }
'GB' { $Bytes = $GB * 1073741824 }
'TB' { $Bytes = $TB * 1099511627776 }
'PB' { $Bytes = $PB * 1125899906842624 }
}
switch ( $Bytes ) {
{$Bytes -ge 1PB} { return '{0:N1} PB' -f ($Bytes / 1PB) }
{$Bytes -ge 1TB} { return '{0:N1} TB' -f ($Bytes / 1TB) }
{$Bytes -ge 1GB} { return '{0:N1} GB' -f ($Bytes / 1GB) }
{$Bytes -ge 1MB} { return '{0:N1} MB' -f ($Bytes / 1MB) }
{$Bytes -ge 1KB} { return '{0:N1} KB' -f ($Bytes / 1KB) }
default { return '{0:N1} B ' -f $Bytes }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment