Powershell has many cmdlets that help users perform various tasks. In this post, I will use several commands to collect CPU and memory usage of processes.

Collect Process CPU Usage

Powershell has a Get-Counter cmdlet. This cmdlet let users to obtain metrics of the system. One of the counters that you can use is “\Processor(*)\% Processor Time“. Open your powershell, and run the following command:

The result looks like:

You can find that the timestamp is just when the user ran this command. What we need is just the CounterSamples part. So with following command, the CounterSamples part is extracted.

This gives a list of all processes with CPU usages. But it is too many for performance monitoring. Therefore, I will restrict the results to those having nonzero CPU usages. This is done with the following command.

This results in something like the following:

The CookedValue column is the “percentage” of CPU use. You can find that the values can be greater than 100. This is because it adds multiple cores. On my PC, there are 16 cores, so it sums to about 1600.

Collect Process Memory Usage

To get memory usage, another cmdlet Get-Process is useful. It goes like as below:

Here, like in the previous section, I used the Where-Object cmdlet to restrict the result to active processes only. And because Get-Process can give a long list of properties of every process, I restrict to two fields. Name is the name of the process. WS is is the working set memory in KB. The output goes like the following:

Name                           WS
----                           --
ChsIME                    7901184
conhost                  16814080
conhost                  17293312
ctfmon                   21581824
dllhost                  13258752
explorer                207691776
LockApp                  61153280
msedge                   20189184
msedge                  289972224
msedge                   17846272
msedge                   29536256
msedge                   47423488
msedge                  106610688
msedge                  123375616
msedge                  125349888
msedge                  168783872
msedge                  128581632
msedge                  191397888
msedge                  104919040
msedge                   21688320
msedge                  120528896
msedge                    8261632
MusNotifyIcon            14864384
notepad++                30425088
powershell               98615296
powershell              101376000
RtkAudUService64          9863168
RuntimeBroker            28770304
RuntimeBroker            24854528
RuntimeBroker            41598976
RuntimeBroker            32833536
SearchApp               266919936
SecurityHealthSystray     9510912
sihost                   29876224
StartMenuExperienceHost  73383936
svchost                  13090816
svchost                  25026560
svchost                  33251328
svchost                   8740864
svchost                  22769664
taskhostw                18632704
TextInputHost            53374976
TSVNCache                 9928704
ttpmenu                  12865536
UserOOBEBroker           10113024

Summary

This article, I mentioned two ways for collecting CPU and memory usages, respectively. These outputs can be treated as input data matrices for producing higher level summaries like identifying usage pattern or bottleneck, etc.

Wish this helpful!