How do I find the CPU and RAM usage using PowerShell?

I am trying to get PowerShell to give me the RAM and CPU usage, but I can't figure out what WMI class to use. My computer has two processors, so it would be useful to have the information for both of them.

6 Answers

You can also use the Get-Counter cmdlet (PowerShell 2.0):

Get-Counter '\Memory\Available MBytes'
Get-Counter '\Processor(_Total)\% Processor Time'

To get a list of memory counters:

Get-Counter -ListSet *memory* | Select-Object -ExpandProperty Counter
2
Get-WmiObject Win32_Processor | Select LoadPercentage | Format-List

This gives you CPU load.

Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select Average
7

I use the following PowerShell snippet to get CPU usage for local or remote systems:

Get-Counter -ComputerName localhost '\Process(*)\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property instancename, cookedvalue| Sort-Object -Property cookedvalue -Descending| Select-Object -First 20| ft InstanceName,@{L='CPU';E={($_.Cookedvalue/100).toString('P')}} -AutoSize

Same script but formatted with line continuation:

Get-Counter -ComputerName localhost '\Process(*)\% Processor Time' ` | Select-Object -ExpandProperty countersamples ` | Select-Object -Property instancename, cookedvalue ` | Sort-Object -Property cookedvalue -Descending | Select-Object -First 20 ` | ft InstanceName,@{L='CPU';E={($_.Cookedvalue/100).toString('P')}} -AutoSize

On a 4 core system it will return results that look like this:

InstanceName CPU
------------ ---
_total 399.61 %
idle 314.75 %
system 26.23 %
services 24.69 %
setpoint 15.43 %
dwm 3.09 %
policy.client.invoker 3.09 %
imobilityservice 1.54 %
mcshield 1.54 %
hipsvc 1.54 %
svchost 1.54 %
stacsv64 1.54 %
wmiprvse 1.54 %
chrome 1.54 %
dbgsvc 1.54 %
sqlservr 0.00 %
wlidsvc 0.00 %
iastordatamgrsvc 0.00 %
intelmefwservice 0.00 %
lms 0.00 %

The ComputerName argument will accept a list of servers, so with a bit of extra formatting you can generate a list of top processes on each server. Something like:

$psstats = Get-Counter -ComputerName utdev1,utdev2,utdev3 '\Process(*)\% Processor Time' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty countersamples | %{New-Object PSObject -Property @{ComputerName=$_.Path.Split('\')[2];Process=$_.instancename;CPUPct=("{0,4:N0}%" -f $_.Cookedvalue);CookedValue=$_.CookedValue}} | ?{$_.CookedValue -gt 0}| Sort-Object @{E='ComputerName'; A=$true },@{E='CookedValue'; D=$true },@{E='Process'; A=$true }
$psstats | ft @{E={"{0,25}" -f $_.Process};L="ProcessName"},CPUPct -AutoSize -GroupBy ComputerName -HideTableHeaders

Which would result in a $psstats variable with the raw data and the following display:

 ComputerName: utdev1 _total 397% idle 358% 3mws 28% webcrs 10% ComputerName: utdev2 _total 400% idle 248% cpfs 42% cpfs 36% cpfs 34% svchost 21% services 19% ComputerName: utdev3 _total 200% idle 200%
2

I have combined all the above answers into a script that polls the counters and writes the measurements in the terminal:

$totalRam = (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).Sum
while($true) { $date = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $cpuTime = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue $availMem = (Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue $date + ' > CPU: ' + $cpuTime.ToString("#,0.000") + '%, Avail. Mem.: ' + $availMem.ToString("N0") + 'MB (' + (104857600 * $availMem / $totalRam).ToString("#,0.0") + '%)' Start-Sleep -s 2
}

This produces the following output:

2020-02-01 10:56:55 > CPU: 0.797%, Avail. Mem.: 2,118MB (51.7%)
2020-02-01 10:56:59 > CPU: 0.447%, Avail. Mem.: 2,118MB (51.7%)
2020-02-01 10:57:03 > CPU: 0.089%, Avail. Mem.: 2,118MB (51.7%)
2020-02-01 10:57:07 > CPU: 0.000%, Avail. Mem.: 2,118MB (51.7%)

You can hit Ctrl+C to abort the loop.

So, you can connect to any Windows machine with this command:

Enter-PSSession -ComputerName MyServerName -Credential MyUserName

...paste it in, and run it, to get a "live" measurement. If connecting to the machine doesn't work directly, take a look here.

To export the output to file on a continuous basis (here every five seconds) and save to a CSV file with the Unix date as the filename:

while ($true) { [int]$date = get-date -Uformat %s $exportlocation = New-Item -type file -path "c:\$date.csv" Get-Counter -Counter "\Processor(_Total)\% Processor Time" | % {$_} | Out-File $exportlocation start-sleep -s 5
}

Here's a method that keeps the last 10 measurements in the registry and returns the average. It could be something like a Kace custom inventory rule that runs every 6 hours. I found CxUIUSvc using a lot of cpu with this.

if (test-path hkcu:\software\cpu) {$cpu = (get-itemproperty hkcu:\software\cpu).cpu}
$cpu = @($cpu + (Get-CimInstance win32_processor).loadpercentage)[-10..-1]
if (! (test-path hkcu:\software\cpu)) {new-item hkcu:\software\cpu > $null}
set-itemproperty hkcu:\software\cpu cpu $cpu -type multistring
($cpu | measure -average).average

Checking on the history with invoke-command:

$FormatEnumerationLimit = 10
icm comp1,comp2,comp3 { get-itemproperty hklm:\software\cpu -ea 0 | select cpu }
cpu PSComputerName RunspaceId
--- -------------- ----------
{2, 1, 2, 91, 7, 3, 0, 1} comp1 9b94ab81-2488-4322-bd8b-6c0afd53f340
{0, 99, 10, 4, 1, 1, 6, 5, 0, 3} comp2 02dbe3ed-c09d-430a-94f2-e55fe0b63b66
{0, 0, 13, 30, 57, 0, 20, 0, 0, 0} comp3 7f488a86-bb06-4b25-aa46-86dea603700d

You Might Also Like