Invoke-BeautifySize.ps1

<#PSScriptInfo
 
.VERSION 1912.0.0
 
.GUID 98e08dbd-4ff9-42a1-9647-7e1c1a5c92a8
 
.AUTHOR Tim Small
 
.COMPANYNAME Smalls.Online
 
.COPYRIGHT 2019
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Convert bytes to a readable string
 
#>

param(
    [Parameter(Position = 1, Mandatory)]
    [int64]$Bytes
)

begin {
    $PrettifyData = @{
        "Size"   = $null;
        "Metric" = $null
    }
}

process {

    switch ($Bytes) {
        { ($PSItem -ge 1PB) } {
            $PrettifyData.Size = ($PSItem / 1PB)
            $PrettifyData.Metric = "PB"
            break
        }

        { ($PSItem -ge 1TB) } {
            $PrettifyData.Size = ($PSItem / 1TB)
            $PrettifyData.Metric = "TB"
            break
        }

        { ($PSItem -ge 1GB) } {
            $PrettifyData.Size = ($PSItem / 1GB)
            $PrettifyData.Metric = "GB"
            break
        }

        { ($PSItem -ge 1MB) } {
            $PrettifyData.Size = ($PSItem / 1MB)
            $PrettifyData.Metric = "MB"
            break
        }

        { ($PSItem -ge 1KB) } {
            $PrettifyData.Size = ($PSItem / 1KB)
            $PrettifyData.Metric = "KB"
            break
        }

        Default {
            $PrettifyData.Size = ($PSItem)
            $PrettifyData.Metric = "B"
            break
        }
    }

    $returnString = "$([math]::Round($PrettifyData.Size, 2)) $($PrettifyData.Metric)"
}

end {
    return $returnString
}