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.
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.
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:
That gets a lot of information. I’d like to see the counters themselves as I would use them with Get-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.
Kris was an employee at PDQ.