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

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
PS C:\Users\boe\Downloads> Convert-BytesToSize 2352323
PS C:\Users\boe\Downloads> Convert-BytesToSize 23523
PS C:\Users\boe\Downloads> Convert-BytesToSize 235232323
PS C:\Users\boe\Downloads> Convert-BytesToSize 23523232323
PS C:\Users\boe\Downloads> Convert-BytesToSize 2352323232323
PS C:\Users\boe\Downloads> Convert-BytesToSize 235232323232323
PS C:\Users\boe\Downloads> Convert-BytesToSize 23523232323232323

Script below:

Function Convert-BytesToSize
Converts any integer size given to a user friendly size.


Converts any integer size given to a user friendly size.


Used to convert into a more readable format.
Required Parameter


ConvertSize -size 134217728
Converts size to show 128MB


#Requires -version 2.0



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


About Boe Prox

Microsoft Cloud and Datacenter MVP working as a SQL DBA.
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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s