
$ProcessCPU = (Get-Counter "\Process($Process*)\% Processor Time").CounterSamples | Sort-Object Path,CPU $ProcessId = (Get-Counter "\Process($Process*)\ID Process").CounterSamples | Sort-Object Path Once that’s done, I’ll store the data that I want and return the list sorted with the process with the highest CPU usage on top: Param($Process) I’ll start by putting the data in variables, then I’ll check if the lists are the same size. Now it’s time to start working with the data. This command will show me the Process ID’s: (Get-Counter "\Process(Chrome*)\ID Process").CounterSamples Lets solve this with the Get-Counter command as well. This’ll give me this output (with a lot of open tabs in chrome):īut now, there’s the problem that the name as being used in the Performance Counters isn’t the same as in the process list, neither do I know a process ID. This is achieved with this command: (Get-Counter "\Process(Chrome*)\% Processor Time").CounterSamples The output is not quite what I want, I only like to get the CounterSamples. But to explain them: The SampleInterval switch will set the interval time between the 2 measuring samples, default is 1 second, the MaxSamples switch will give you an x amount of samples the default value is 1 sample. If you’d want to take multiple samples, this can also be specified (just like the sample interval) Get-Counter "\Process(Chrome*)\% Processor Time" -SampleInterval 1 -MaxSamples 5Īt the moment I’m only interested in getting 1 quick sample (this script will be integrated in a monitoring tool), thus I’ll skip these switches. This gives me a list of all process instances and their current CPU usage.

I’d like to get % processor time information, thus I’ll check this with the following command (add the * to get all instances of the process): Get-Counter "\Process(Chrome*)\% Processor Time" I finally got to the Get-Counter command, which gets information from Performance Counters. WMI has many possibilities, but most of my scripts that are lagging, are lagging because of WMI usage, thus I try to use this only if I can’t find another way to do so.

#Windows powershell using cpu how to
If you’re interested in this information, explains how to do this. This is not the information I want to know, thus I started searching for another way to do it.
#Windows powershell using cpu software
(So if it started at 100% CPU usage and has been busy since, it’ll show as 100%, but when it’s software that’s been started days ago and just been using 100% for the past 10 minutes, without doing much the time before, it’ll show as the program using 0,05 % or something like that, since that is its average usage). Thus if you use the previous information you got from the tool to calculate the CPU usage, you’ll get the average CPU usage for that process since it started. The Get-Process command will give you CPU time and program start time, but will not tell you when the CPU was being used by the process. I wanted to get the current CPU usage for certain processes, which appeared to be harder than I thought.
