Find the CPU and RAM Usage Using PowerShell

Rohan Timalsina Mar 09, 2022
  1. Use Get-Counter to Find the CPU and RAM Usage Using PowerShell
  2. Use Get-WMIObject to Find the CPU and RAM Usage Using PowerShell
Find the CPU and RAM Usage Using PowerShell

Windows administrators have to monitor the performance of computers and servers’ machines to check for issues. It is difficult and time-consuming to log in to multiple computers to find the CPU and RAM utilization.

This tutorial will introduce different methods to find the CPU and RAM usage with PowerShell on the local computers.

Use Get-Counter to Find the CPU and RAM Usage Using PowerShell

The Get-Counter cmdlet gets performance data from local and remote computers. It shows data directly from the performance monitoring instrumentation in the Windows operating systems.

Script:

Get-Counter '\Memory\Available MBytes'

Output:

Timestamp                  CounterSamples
---------                  --------------
2/27/2022 12:10:02 AM      \\laptop-d045jb6g\memory\available mbytes :
                           6958

The following command gets the processor time of the local computer.

Script:

Get-Counter '\Processor(_Total)\% Processor Time'

Output:

Timestamp                  CounterSamples
---------                  --------------
2/27/2022 12:11:22 AM      \\laptop-d045jb6g\processor(_total)\% processor time :
                           1.95955145342466

With the -Continuos parameter, you can continuously get the performance data every second and only stop until you press CTRL+C.

Script:

Get-Counter '\Processor(_Total)\% Processor Time' -Continuous

Use Get-WMIObject to Find the CPU and RAM Usage Using PowerShell

The Get-WmiObject cmdlet gets instances of WMI classes or information of the available WMI classes. The following command shows how to find the RAM usage on the local computer.

Script:

$CompObject =  Get-WmiObject -Class WIN32_OperatingSystem
$RAM = (($CompObject.TotalVisibleMemorySize - $CompObject.FreePhysicalMemory)/1024/1024)
Write-Host "RAM usage in GB:" $RAM

Output:

RAM usage in GB: 13.117115020752

The Get-WmiObject -Class Win32_OperatingSystem gets the local computer object in the above code. The TotalVisibleMemorySize property gets the system’s total usable memory in KB.

The FreePhysicalMemory property gets the system’s free memory in KB. Then we calculated the used memory, converted it into GB, and saved it in a $RAM variable.

This last command example output is to get the CPU load percentage on the computer.

Script:

Get-WmiObject -Class Win32_Processor | Select LoadPercentage

Output:

LoadPercentage
--------------
            12

Hoping this tutorial has helped you understand how to find the CPU and RAM used in PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website