Techibee.com

Convert from any-to-any (Bytes, KB, MB, GB, TB) using PowerShell

We all know that size conversion in PowerShell is pretty straightforward. If you have a number in bytes and want to convert it into MB or GB, it is as simple as typing 12345/1GB or 12345/1MB in PowerShell prompt or script.

Ok, then where is the problem and why I am writing this post. The real problem comes in two scenarios.

Scenario#1

When you want to convert a value from other than bytes to KB/MB/TB/GB: It is easy to convert any format to other using PowerShell console but when it comes to writing scripts, we will have this problem. For example, VMware datastores will have a property which says CapacityinMB and the value will be integer. In that case, you need to convert the number to bytes and then convert back to your final required format. This is because, from property name you know it is in MB but there is no easy way to make the shell understand that is in MB while doing the conversion. Conversion like 245760/1GB treats 245760 as bytes and calculation will go wrong. We can follow a ugly way like suffixing the number with a string “MB” and then divide it by 1GB (“245760MB”/1GB). I feel this not a good programming approach.

Scenario#2

When want to apply precision and return only 2(or any given number) of digits after the dot: Another problem you will notice is the no. of digits you want to have in the precision numbers. When converting using the traditional method, we will end up with more number of precision digits which I want to avoid. I just need 2 or 3 based on my requirement. Again we can address this by using a ugly method by calling Tostring(“00”)

To address above two scenarios, I came with below function which eases the conversion from any-to-any (Bytes, KB, MB, GB, and TB). It also has a facility mention the no. of precision digits you want to have.

Code#

function Convert-Size {            
[cmdletbinding()]            
param(            
    [validateset("Bytes","KB","MB","GB","TB")]            
    [string]$From,            
    [validateset("Bytes","KB","MB","GB","TB")]            
    [string]$To,            
    [Parameter(Mandatory=$true)]            
    [double]$Value,            
    [int]$Precision = 4            
)            
switch($From) {            
    "Bytes" {$value = $Value }            
    "KB" {$value = $Value * 1024 }            
    "MB" {$value = $Value * 1024 * 1024}            
    "GB" {$value = $Value * 1024 * 1024 * 1024}            
    "TB" {$value = $Value * 1024 * 1024 * 1024 * 1024}            
}            
            
switch ($To) {            
    "Bytes" {return $value}            
    "KB" {$Value = $Value/1KB}            
    "MB" {$Value = $Value/1MB}            
    "GB" {$Value = $Value/1GB}            
    "TB" {$Value = $Value/1TB}            
            
}            
            
return [Math]::Round($value,$Precision,[MidPointRounding]::AwayFromZero)            
            
}            
            

Output:

The output is very intuitive and explains the usage.

Hope this helps…

Exit mobile version