Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ConvertTo-ByteUnits
- {
- [CmdletBinding()]
- Param (
- [Parameter (
- Position = 0,
- Mandatory)]
- [int64]
- $Size
- )
- begin {}
- process
- {
- $Sign = [math]::Sign($Size)
- $Size = [math]::Abs($Size)
- switch ($Size)
- {
- {$_ -gt 1TB }
- {$Unit = 'TB'; break}
- {$_ -gt 1GB }
- {$Unit = 'GB'; break}
- {$_ -gt 1MB }
- {$Unit = 'MB'; break}
- {$_ -gt 1KB }
- {$Unit = 'KB'; break}
- default
- {$Unit = 'B'}
- }
- if ($Unit -ne 'B')
- {
- '{0:N2} {1}' -f ($Sign * $Size / "1$Unit"), $Unit
- }
- else
- {
- '{0} {1}' -f ($Sign * $Size), $Unit
- }
- } # end >> process
- end {}
- } # end >> function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement