Home » VB.NetRSS

How do I get CPU usage from WMI?

Hi people,

I started using WMI to get Drive, Memory, and CPU information. But I am not sure or don't know how to get CPU usage. This is part of a system monitoring functionality I am trying to implement. I need CPU's usage like the same as you get from Task Manager(in percentage). I don't need CPU's temperature.

Also, I need total memory(as in RAM). This is the same RAM from My Computer >> Properties. Example: 2.99GB of RAM. I got FreePhysicalMemeory and FreeVirtualMemeory from Win32_OperatingSystem but I still need the total RAM. I am thinking FreeVirtualMemory is from HardDrive and FreePhysicalMemory is from the RAM that is not being used, and thus free for use.



15 Answers Found


Answer 2

Just a small addtion to the previous great link:

Computer Memory you can get from Win32_ComputerSystem -> TotalPhysicalMemory
CPU counter you can get from Win32_PerfFormattedData_Counters_ProcessorInformation -> PercentProcessorTime; the first collection entry returns total  for all processors and cores, the 2nd entry all cores of the first processor. If the system  has just one processor they are the same


Answer 3

Something simple like this will work.

Forward Declarions:

	private: System::Management::SelectQuery^ Info
	private: System::Management::ManagementObjectSearcher^ Searcher;
 private: int cpuUsage (System::Object^ sender, System::EventArgs^ e)
this->Info = gcnew System::Management::SelectQuery("Win32_PerfFormattedData_PerfOS_Processor ");
this->Searcher = gcnew System::Management::ManagementObjectSearcher(Info);
Object^ idleTime;
ManagementObject^ envVar;
for each (envVar in Searcher->Get()) { idleTime = envVar["PercentIdleTime"]; } return 100 - int::Parse(idleTime->ToString()); }

Answer 4

Hi UgnV,

This looks good but can you get me code in VB? if possible?

I can try the Win32_ComputerSystem -> TotalPhysicalMemory first. I assume this will give me the total  RAM; the same RAM as you get from My Computer -> Properties? But how do I get total free  RAM?

I will need some code to get the percentage  of CPU's usage  from Win32_PerfFormattedData_Counters_ProcessorInformation -> PercentProcessorTime.



Answer 5

Thanks for replying pyrox,

I am not sure how to implement  your code. I assume its in c++ and I don't see the return value that I needed. I need total  RAM and total free  RAM.

And I also need CPU's usage  in percentage.



Answer 6

Download the WMI Creator!!

It generates code in several languages.


Answer 7

Hi Tergiver,

I downloaded and ran the creator. There's 472 classes. How the heck do I know which one to run? Besides the classes mentioned above?

As of now, I got the total  RAM. I still need the CPU's usage  in percentage.



Answer 8

Hi Tergiver,

The Creator have no: 


under: root\CIMV2

Anyway, I got an error from the following when I tried to grab CPU's usage  from below:

Dim scope5 = New ManagementScope()
    Dim queryBuilder5 = New StringBuilder()

    queryBuilder5.Append("SELECT PercentProcessorTime FROM Win32_PerfFormattedData_Counters_ProcessorInformation")

    Dim query5 = New ObjectQuery(queryBuilder5.ToString())
    Dim search5 = New ManagementObjectSearcher(scope5, query5)

    Dim collectionOfResults5 As ManagementObjectCollection

    collectionOfResults5 = search5.Get()

    Dim currentObject5 As ManagementObject

    'Display Total RAM information
    For Each currentObject5 In collectionOfResults5
      '_collected.AddFirst("CPU's Usage(%): " + currentObject5("PercentProcessorTime").ToString()))
      lbDiskInformation.Items.Add("CPU's Usage(%): " + currentObject5("PercentProcessorTime").ToString())

I got an error at the end(at the For Each loop). The collectionOfResults5 yield nothing.




Answer 9

Hi BlueMarker,

To get free memory  you can use Win32_PerfFormattedData_PerfOS_Memory ->AvailableBytes or AvailableMBytes
Below is the working code in VB that gets CPU percentage  sample and free  memory

Imports System
Imports System.Management
'Imports System.Windows.Forms

Module Module1

  Sub Main()


  End Sub

End Module

Namespace WMISample

  Public Class MyWMIQuery

    Public Overloads Shared Function Main() As Integer

      Dim processorSearcher As New ManagementObjectSearcher( _
       "root\CIMV2", _
       "SELECT * FROM Win32_PerfFormattedData_Counters_ProcessorInformation")

      For Each queryObj As ManagementObject In processorSearcher.Get()

        Console.WriteLine("PercentProcessorTime: {0}", queryObj("PercentProcessorTime"))

        ' for system  with single processor we exit after the 1st iteration
        Exit For

      Dim memorySearcher As New ManagementObjectSearcher( _
        "root\CIMV2", _
        "SELECT * FROM Win32_PerfFormattedData_PerfOS_Memory")

      For Each queryObj As ManagementObject In memorySearcher.Get()
        Console.WriteLine("AvailableBytes: {0}", queryObj("AvailableBytes"))
        Console.WriteLine("AvailableMBytes: {0}", queryObj("AvailableMBytes"))

    End Function
  End Class
End Namespace

Best Regards,


Answer 10

Thanks for the help.

Ugnv pointed to the appropiate class and code which help me.

Tergives pointed me the the WMI Creator which is helpful but I still need to know which class and which value to select.

I will mark both as answer.

Thanks again,



Answer 11

There's 472 classes. How the heck do I know which one to run?

I know you're going to hate this answer, but I love giving it:



Answer 12

There's 472 classes. How the heck do I know which one to run?
That is called connecting the dots.  Get used  to it.  ;)

Answer 13

Already got this saved in My Favorites...

I was on my way to the answer...just need the needle in the hay stack :)



Answer 14

Yea, this could be fun. Problem is: I got a little deadline. Learn something new everyday....that's for sure.

Answer 15

Already got this saved in My Favorites...

I was on my way to the answer...just need the needle in the hay stack :)


Save a link in your code inside of a comment.

// links

// http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/608a2bc0-3bf8-4845-b0d8-09f2624189f7#bb899507-8c45-478a-b484-3e381b933d22


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter