Private/ConvertSize.ps1

function ConvertSize {
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$True)]
        [validateset("Bytes","KB","MB","GB","TB")]
        [string]$From,

        [Parameter(Mandatory=$True)]
        [validateset("Bytes","KB","MB","GB","TB")]
        [string]$To,

        [Parameter(Mandatory=$True)]
        [double]$Value,

        [Parameter(Mandatory=$False)]
        [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)
}