I know how to get total physical memory from win32_computersystem class. but that comes in bytes or kb. I want this information in MB or GB. in wmi (wql) query. wmic also work. thanks in advance.
-
1So, why don't you convert it yourself? (if you're coding, in code, otherwise, paste it to Excel with a formula or something like that...) – DigCamara Apr 16 '13 at 15:59
-
In case you might be looking for some other way to get the RAM size: http://www.commonfixes.com/2014/12/get-systems-physical-ram-using-csharp.html – Farhan S. Nov 01 '15 at 13:18
4 Answers
you can convert TotalPhysicalMemory
of Win32_ComputerSystem. Try this :
using System;
using System.Management;
namespace WMISample
{
public class MyWMIQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT TotalPhysicalMemory FROM Win32_ComputerSystem");
foreach (ManagementObject queryObj in searcher.Get())
{
double dblMemory;
if(double.TryParse(Convert.ToString(queryObj["TotalPhysicalMemory"]),out dblMemory))
{
Console.WriteLine("TotalPhysicalMemory is: {0} MB", Convert.ToInt32(dblMemory/(1024*1024)));
Console.WriteLine("TotalPhysicalMemory is: {0} GB", Convert.ToInt32(dblMemory /(1024*1024*1024)));
}
}
}
catch (ManagementException e)
{
}
}
}
}

- 9,694
- 6
- 38
- 61
You must convert the value of the property manually. Also is better use Win32_PhysicalMemory WMI class.
Try this sample
using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
namespace GetWMI_Info
{
class Program
{
static void Main(string[] args)
{
try
{
ManagementScope Scope;
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", "."), null);
Scope.Connect();
ObjectQuery Query = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
UInt64 Capacity = 0;
foreach (ManagementObject WmiObject in Searcher.Get())
{
Capacity+= (UInt64) WmiObject["Capacity"];
}
Console.WriteLine(String.Format("Physical Memory {0} gb", Capacity / (1024 * 1024 * 1024)));
Console.WriteLine(String.Format("Physical Memory {0} mb", Capacity / (1024 * 1024)));
}
catch (Exception e)
{
Console.WriteLine(String.Format("Exception {0} Trace {1}", e.Message, e.StackTrace));
}
Console.WriteLine("Press Enter to exit");
Console.Read();
}
}
}

- 134,889
- 20
- 356
- 483
Wanted to make mention that I used the Win32_PhysicalMemory Capacity property until I met inconsistent results on Windows Server 2012. Now I use both properties (Win32_ComputerSystem:TotalPhysicalMemory and Win32_PhysicalMemory:Capacity) and choose the larger of the two.

- 31
- 1
-
2Welcome to Stack Overflow! This is really a comment and **not** an answer to the original question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – DavidPostill Mar 18 '15 at 12:13
For anyone that lands here now (this post is 10 years old)
Get-WmiObject is deprecated in PowerShell 3.0 and above. ( Source : https://ss64.com/ps/get-wmiobject.html)
Get-WmiObject is being replaced with Get-CimInstance, which is so much better in many ways!
If you are still wanting to do this, please consider using the following -
Code
$MemoryUncleaned = (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).sum /1gb
$Memory = $MemoryUncleaned.ToString() + "GB"
$Memory
Output
64GB

- 21
- 2