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.
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
get-wmiobject Win32_PerfFormattedData_PerfProc_Process| Select-Object -Property Name, PercentProcessorTime
Apr 8, 2014 at 16:54
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}