How to use PowerShell to get CPU usage for a process using Get-Counter cmdlet

Black and White PDQ logo
Kris Powell|Updated January 20, 2021
Powershell: Get CPU Usage for a Process Using Get-Counter
Powershell: Get CPU Usage for a Process Using Get-Counter
Sections
    No Data

    I was on Stack Overflow not too long ago when I came across a post involving gathering CPU usage by percentage that I simply couldn’t pass up. I had to see how PowerShell would handle it.

    I wasn’t disappointed.

    After a few revisions, here is a neat way to get the CPU usage of a particular process and have it displayed by CPU percentage.

    Be sure to choose between option A or option B. I currently have option B commented out.

    ############################################################################# # Option A: This is if you just have the name of the process; partial name OK $ProcessName = "cpu" # Option B: This is for if you just have the PID; it will get the name for you #$ProcessPID = "6860" #$ProcessName = (Get-Process -Id $ProcessPID).Name $CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors $Samples = (Get-Counter "\Process($Processname*)\% Processor Time").CounterSamples $Samples | Select ` InstanceName, @{Name="CPU %";Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}} #############################################################################

    This makes use of the Get-Counter cmdlet that allows you to get performance counter data for local/remote machines. Let’s find out more! The following command will get you a lot of information about all the counter sets that are available to use.

    Get-Counter -ListSet *

    It’s a lot of information. Too much for me, in fact, which is why I usually use the following command. It will get you a list of all the sets of counters that are available to be used, sorted with Sort-Object: (see also Format-Table)

    Get-Counter -ListSet * | Sort-Object CounterSetName | Format-Table CounterSetName

    In that list, I see an object called Processor that I’d like to know more information about and which counters are available. I would find out more information like this:

    Get-Counter -ListSet Processor

    That gets a lot of information. I’d like to see the counters themselves as I would use them with Get-Counter:

    (Get-Counter -ListSet Processor).Counter

    Now we should have a list that includes “\Processor(*)\% Processor Time” This is what I used to find out the percentage of CPU time for the process. The asterisk (*) in this case is a wildcard grabbing all processes. In the script above, we populate that with the process name that we’d like the know the CPU percentage for.

    We also make use of the Get-WMIObject cmdlet to get the total logical processors for the system. This cmdlet is one of the most useful in PowerShell and we will likely post many blogs that include Get-WMIObject. Due to how much information can be gleaned from a system by using this cmdlet, we will not go into too much detail at this point in time.

    Did you know that PDQ Deploy has a PowerShell step you can use to deploy your scripts?

    Black and White PDQ logo
    Kris Powell

    Kris was an employee at PDQ.

    Related articles