System Memory, Units, and Rounding

by Dec 19, 2016

Sometimes, you’d like to use different units of measurements. The total system memory is reported in bytes, for example. Here are some examples how you can turn bytes into GB and still make the result look nice:

$memory = Get-WmiObject -Class Win32_ComputerSystem | 
  Select-Object -ExpandProperty TotalPhysicalMemory

$memoryGB = $memory/1GB

# raw result in bytes
$memoryGB

# rounding
[Int]$memoryGB
[Math]::Round($memoryGB)
[Math]::Round($memoryGB, 1)

# string formatting
'{0:n1} GB' -f $memoryGB

The result looks similar to this:

 
15.8744087219238
16
16
15.9
15.9 GB
 

Twitter This Tip! ReTweet this Tip!