5

I want to get the value that is shown in task manager for any process in the CPU column in powershell.

I tried using

Get-Process ProcessName | Select-Object -Property CPU

but it only returns the time spent.

1

3 Answers 3

1

Try using the Get-Counter command which pulls the data from the system's performance monitor. For your example, it would look like this:

# ~> Get-Counter "\Process(ProcessName*)\% Processor Time" | select -expand countersamples 

An example, using chrome:

# ~> Get-Counter "\Process(chrome*)\% Processor Time" | select -expand countersamples 

Path                                            InstanceName      CookedValue
----                                            ------------      -----------
\\machinename\process(chrome#7)\% processor time chrome                      0
\\machinename\process(chrome#6)\% processor time chrome                      0
\\machinename\process(chrome#5)\% processor time chrome                      0
\\machinename\process(chrome#4)\% processor time chrome                      0
\\machinename\process(chrome#3)\% processor time chrome                      0
\\machinename\process(chrome#2)\% processor time chrome                      0
\\machinename\process(chrome#1)\% processor time chrome                      0
\\machinename\process(chrome)\% processor time   chrome       3.10141153081511
2
  • 1
    Thanks for the post but I got it working by the code : get-wmiobject Win32_PerfFormattedData_PerfProc_Process| Select-Object -Property Name, PercentProcessorTime Apr 8, 2014 at 16:54
  • How do I get it to also show the process ID? I find it useless without the ID because I have multiple processes running that are the same name and need to know which one is stuck using the CPU so I don’t kill the wrong one.
    – Aenfa
    Feb 22, 2022 at 15:42
1

Here are some copy-pastable examples of filtering processes with Get-Counter and Get-WmiObject.

For example, to get the top 10 processes by CPU usage:

powershell "(Get-Counter '\Process(*)\% Processor Time').Countersamples | Sort cookedvalue -Desc| Select -First 10 instancename, cookedvalue"

Or, with cleaner formatting:

powershell "(Get-Counter '\Process(*)\% Processor Time').Countersamples | Sort cookedvalue -Desc | Select -First 10 instancename, @{Name='CPU %';Expr={[Math]::Round($_.CookedValue)}}"


powershell "gwmi Win32_PerfFormattedData_PerfProc_Process | Sort PercentProcessorTime -desc | Select -first 7 Name, PercentProcessorTime, IOReadBytesPersec, IOWriteBytesPersec, WorkingSet | ft -autoformat"
0

The simpler way I would go on achieving this is

get-process processName | where-object {$_.cpu -gt desirevalue}
1
  • 1
    This is very similar to the command that OP used originally and found that it didn't work for them. Could you explain what is different in your answer? Please see How to Answer and take our tour.
    – Burgi
    May 11, 2021 at 14:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .