Win32_OperatingSystem.FreePhysicalMemory and $_.TotalVisibleMemory giving output in wrong units
I've been putting together a pretty simple script to monitor some aspects of our Terminal Server farm usage and am implementing a section where I check the memory usage on the servers at a given point in time. Here is the particular section that I am using to get that:
<#Modified to troubleshoot this particular section; defined $TermSvr and pipe output directly to
host:#>
$RemoteSvr = "Win10Test"
#Check current Memory Usage and Available Space
$SysMem = Get-WmiObject Win32_OperatingSystem -ComputerName $RemoteSvr
"$RemoteSvr has {0:#.0} GB free space out of {1:#.0} GB total available memory" -f
($SysMem.FreePhysicalMemory/1GB),
($SysMem.TotalVisibleMemorySize/1GB) | Write-Host
This outputs:
Win10Test has **.0** GB free space out of **.0** GB total available memory
But; when I change the ($_.SysMem.TotalVisibleMemorySize/1GB) to ($_.SysMem.TotalVisibleMemorySize/1MB)
It outputs:
Win10Test has 1.1 GB free space out of 3.8 GB total available memory
Which is correct. But I feel like I'm taking crazy pills at this point. Am I missing something simple here to explain why these are only returning the value which translates into MEGABYTES of memory as opposed to the actual GIGABYTES of memory I have on the systems?
I have tried running this script against:
- Windows 8.1
- Windows 10 (Tech Preview)
- Windows Server 2012 R2
Always the same outcome.
Solution 1:
According to Win32_OperatingSystem class on MSDN:
TotalVisibleMemorySize
Data type: uint64
Access type: Read-only
Total amount, in kilobytes, of physical memory available to the operating system.
Of course the same is true for FreePhysicalMemory
.
Dividing by 1GB
in PowerShell is the equivalent of dividing by 1073741824
(or by 1024*1024*1024
). Hence, the amount of memory would need to be expressed in bytes in order for division by 1GB to return an amount of RAM in GB.
Since TotalVisibleMemorySize
is in kilobytes, you can convert to GB by:
TotalVisibleMemorySize/1MB
or
TotalVisibleMemorySize*1024/1GB