Need help in converting KB to GB

Hi Team,

Could anyone please help me to find out what I am doing wrong in below cmdlet:

I am converting below properties in GB:

Get-WmiObject -Class Win32_OperatingSystem | Select-Object TotalVisibleMemorySize,FreePhysicalMemory

Below is the result of above command:

TotalVisibleMemorySize FreePhysicalMemory


          16776760                       7170064

This result is showing in KB.
Now I am converting this data in GB’s with below cmdlet:

Get-WmiObject -Class Win32_OperatingSystem | Select-Object CSName, @{Name=“TotalRAM”;expression={$.TotalVisibleMemorySize / 1GB }},@{Name=“FreeRAM”;expression={$.FreePhysicalMemory / 1GB }}: Result is below:

CSName TotalRAM FreeRAM


Servername 0.015624575316906 0.00665861368179321

Seems it is assuming that data is in bytes and converting them from bytes to GB’s. Then I ran below cmdlet:

Get-WmiObject -Class Win32_OperatingSystem | Select-Object CSName, @{Name=“TotalRAM”;expression={$.TotalVisibleMemorySize / 1kb / 1GB }},@{Name=“FreeRAM”;expression={$.FreePhysicalMemory / 1kb / 1GB }}: Below is the result:

CSName TotalRAM FreeRAM


Servername 1.5258374332916E-05 6.4604937506374E-06

Below is the data from task manager:

Physical Memory (MB)
Total 16383
Cached 6612
Available 7163
Free 601

Please help me to convert data in GB’s. Let me know if any further information is required.

Thanks
Jatinder

Hi Team,

Could anyone please help me to find out what I am doing wrong in below cmdlet:

I am running the below command:

Get-WmiObject -Class Win32_OperatingSystem | Select-Object CSName,TotalVisibleMemorySize,FreePhysicalMemory: Below is the result:

CSName TotalVisibleMemorySize FreePhysicalMemory


Servername 16776760 7204180

The above data is showing in KBs.

Then I am converting the data with below cmdlet:

Get-WmiObject -Class Win32_OperatingSystem | Select-Object CSName, @{Name=“TotalRAM”;expression={$.TotalVisibleMemorySize / 1GB }},@{Name=“FreeRAM”;expression={$.FreePhysicalMemory / 1GB }}

CSName TotalRAM FreeRAM


Servername 0.015624575316906 0.00669724866747856

It seems that is assuming the data in bytes and converting it from bytes to GBs. Then I again ran below command and result is:

Get-WmiObject -Class Win32_OperatingSystem | Select-Object CSName, @{Name=“TotalRAM”;expression={$.TotalVisibleMemorySize / 1KB / 1GB }},@{Name=“FreeRAM”;expression={$.FreePhysicalMemory / 1KB / 1GB }}

CSName TotalRAM FreeRAM


LON2INFTS01 1.5258374332916E-05 6.7107321228832E-06

Below is the data from taskmgr:

Physical Memory(MB)
Total 16383
Cached 6608
Available 7074
Free 560

Please help me converting the data to GB’s.

What is it that you don’t like about the result? Are you looking for fewer decimal places? The -f formatting operator would be a way to achieve that.

Hi Don,

I am not understanding the below result:

TotalRAM: 1.5258374332916E-05
FreeRAM: 6.7107321228832E-06

Server has 16GB of RAM and available is 7GB approx.

The FreeRAM part looks Ok but TotalRAM result does not match. Please confirm if I am thinking anything wrong.

Converting from KB to GB is to divide by MB (10^3 to 10^9 ==> 10^-6)

Get-WmiObject -Class Win32_OperatingSystem | 
    Select-Object CSName, 
        @{Name="TotalRAM";expression={$_.TotalVisibleMemorySize / 1MB }},
        @{Name="FreeRAM"; expression={$_.FreePhysicalMemory / 1MB }}

To round the result:

Get-WmiObject -Class Win32_OperatingSystem | 
    Select-Object CSName, 
        @{Name="TotalRAM";expression={[Math]::Round($_.TotalVisibleMemorySize / 1MB,0) }},
        @{Name="FreeRAM"; expression={[Math]::Round($_.FreePhysicalMemory / 1MB,0) }}

Thank you so much Sam :slight_smile: