Advertisement
Lee_Dailey

function ConvertTo-ByteUnits

Dec 7th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ConvertTo-ByteUnits
  2.     {
  3.     [CmdletBinding()]
  4.     Param (
  5.         [Parameter (
  6.             Position = 0,
  7.             Mandatory)]
  8.             [int64]
  9.             $Size
  10.         )
  11.  
  12.     begin {}
  13.  
  14.     process
  15.         {
  16.         $Sign = [math]::Sign($Size)
  17.         $Size = [math]::Abs($Size)
  18.         switch ($Size)
  19.             {
  20.             {$_ -gt 1TB }
  21.                 {$Unit = 'TB'; break}
  22.             {$_ -gt 1GB }
  23.                 {$Unit = 'GB'; break}
  24.             {$_ -gt 1MB }
  25.                 {$Unit = 'MB'; break}
  26.             {$_ -gt 1KB }
  27.                 {$Unit = 'KB'; break}
  28.             default
  29.                 {$Unit = 'B'}
  30.             }
  31.        
  32.         if ($Unit -ne 'B')
  33.             {
  34.             '{0:N2} {1}' -f ($Sign * $Size / "1$Unit"), $Unit
  35.             }
  36.             else
  37.             {
  38.             '{0} {1}' -f ($Sign * $Size), $Unit
  39.             }
  40.            
  41.         } # end >> process
  42.  
  43.     end {}
  44.  
  45.     } # end >> function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement