Convert bytes to highest available unit

Unfortunately, I had something else in mind to post this weekend, but life and some other things prevented me from doing so.  Maybe next weekend…

Because I wanted to get at least something out this weekend, I am going to use an advanced function I wrote during the 2010 Scripting Games that takes a size in bytes that would normally get if you do a WMI query against something like the Win32_LogicalDisk class for the size of free space and total space on each drive. You would get something like this:

PS C:\temp> gwmi win32_logicaldisk | Select DeviceID,FreeSpace, SizeDeviceID                                                              FreeSpace                                    Size
——–                                                              ———                                    —-
C:                                                                  88777068544                            309185212416
D:                                                                   4628267008                             10737414144
E:

Now this is fine and all but it leaves little to be desired when it comes to readability. Given, can do something like the following to get a specific size, such as GB:

PS C:\temp> gwmi win32_logicaldisk | Select DeviceID,@{Expression={$_.Freespace /1GB};Label=”FreeSpaceGB”}, @{Expression=
$_.Size / 1GB};Label=”SizeGB”}
DeviceID                                                              FreeSpaceGB                                    SizeGB

——–                                                              ———                                    —-

C:                                                              82.679988861084                        287.951168060303

D:                                                             4.31040954589844                        9.99999618530273

E:                                                                            0                                       0

Better, as long as you label somewhere what the default size is, someone who views this report will know what the size is of each hard drive.

What I wrote will take the size in bytes that will the given size and convert it to the highest possible size up to Petabyte. Not only that, but it will add the appropriate size after the integer so exactly what the size is.

PS C:\Users\boe\Downloads> gwmi win32_logicaldisk | Select DeviceID,@{Expression={Convert-BytesToSize $_.Freespace};Labe
l=”FreeSpace”}, @{Expression={Convert-BytesToSize $_.Size};Label=”Size”}

DeviceID                                FreeSpace                               Size
——–                                ———                               —-
C:                                      82.68GB                                 287.95GB
D:                                      4.31GB                                  10GB
E:                                      0Bytes                                  0Bytes


As you can see, the size has been converted to GB and beside each size is the corresponding GB that lets the user know exactly what the size is.  Below are some examples of each size having been converted from its initial bytes size.

PS C:\Users\boe\Downloads> Convert-BytesToSize 235
235Bytes
PS C:\Users\boe\Downloads> Convert-BytesToSize 2352323
2.24MB
PS C:\Users\boe\Downloads> Convert-BytesToSize 23523
22.97KB
PS C:\Users\boe\Downloads> Convert-BytesToSize 235232323
224.34MB
PS C:\Users\boe\Downloads> Convert-BytesToSize 23523232323
21.91GB
PS C:\Users\boe\Downloads> Convert-BytesToSize 2352323232323
2.14TB
PS C:\Users\boe\Downloads> Convert-BytesToSize 235232323232323
213.94TB
PS C:\Users\boe\Downloads> Convert-BytesToSize 23523232323232323
20.89PB

Script below:

Function Convert-BytesToSize
{
<#
.SYNOPSIS
Converts any integer size given to a user friendly size.

.DESCRIPTION


Converts any integer size given to a user friendly size.

.PARAMETER size


Used to convert into a more readable format.
Required Parameter

.EXAMPLE


ConvertSize -size 134217728
Converts size to show 128MB

#>

#Requires -version 2.0


[CmdletBinding()]
Param
(
[parameter(Mandatory=$False,Position=0)][int64]$Size

)


#Decide what is the type of size
Switch ($Size)
{
{$Size -gt 1PB}
{
Write-Verbose “Convert to PB”
$NewSize = “$([math]::Round(($Size / 1PB),2))PB”
Break
}
{$Size -gt 1TB}
{
Write-Verbose “Convert to TB”
$NewSize = “$([math]::Round(($Size / 1TB),2))TB”
Break
}
{$Size -gt 1GB}
{
Write-Verbose “Convert to GB”
$NewSize = “$([math]::Round(($Size / 1GB),2))GB”
Break
}
{$Size -gt 1MB}
{
Write-Verbose “Convert to MB”
$NewSize = “$([math]::Round(($Size / 1MB),2))MB”
Break
}
{$Size -gt 1KB}
{
Write-Verbose “Convert to KB”
$NewSize = “$([math]::Round(($Size / 1KB),2))KB”
Break
}
Default
{
Write-Verbose “Convert to Bytes”
$NewSize = “$([math]::Round($Size,2))Bytes”
Break
}
}
Return $NewSize

}

This entry was posted in powershell, scripts and tagged , , , . Bookmark the permalink.

3 Responses to Convert bytes to highest available unit

  1. Someone says:

    thanks, useful!

  2. jrich says:

    been using this a lot, thanks!

Leave a comment