Get CPU for process in powershell

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 CPU

but it only returns the time spent.

Best Answer

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
Related Question