Byte Math with PowerShell

A lot of powershell modules accept byte values in different formats. For example a utility might request values in bytes or megabytes. In addition, some utilities require values that are powers of two. Doing the math can and converting to and from the right factors can be tedious. So, I wrote ByteCalculationFunctions.ps1 to make it easier.

Import the functions into a PowerShell session and use them:


Invoke-WebRequest https://mig.us/bcfps1 | Invoke-Expression

Get an integer representation of the number of bytes in 9 gigabyte:


bytesfromgigs 9

Use the value with the Hyper-V Set-VMMemory command run:


Set-VMMemory -StartupBytes (4| bytesfromgigs) -MinimumBytes (2| bytesfromgigs) MyVirtualMachine

Convert values:


1024 | bytestok # 1
7 | bytesfromgigs| bytestomegs # 7168
259072 | bytesfrommegs| bytestogigs # 253
768 | bytesfrommegs| bytestok # 786432