Get CPU for process in powershell
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 CPUbut it only returns the time spent.
13 Answers
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 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" The simpler way I would go on achieving this is
get-process processName | where-object {$_.cpu -gt desirevalue} 1