I am able to use the answer CPU percentage.
While(1) { $p = get-counter '\Process(*)\% Processor Time'; cls;
$p.CounterSamples | sort -des CookedValue | select -f 15 | ft -a} Now I'm having problems with being able to have memory usage and CPU percentage displayed at the same time for a task on the computer.
I know Get-Counter "\Process(*)\Working Set - Private" will get the memory but id like to have task, CPU % and Memory usage being displayed at the same time.
UPDATE 1: Ideally I'd like to be able to run this in powershell as a set of commands. I would like the output to be displayed as:
Process Name CPU (%) Memory (MB)
------------ ------- -----------
I will take what I can get when it come to display that can be handled after being able to just displaying the three items in powershell.
UPDATE 2: This section of code provides what I would like
while (1) {cls; Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | select-object -property Name, PercentProcessorTime, IDProcess, @{"Name" = "WSP(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}} | ? {$_.Name -notmatch "^(idle|_total|system)$"} | Sort-Object -Property PercentProcessorTime -Descending| ft -AutoSize | Select-Object -First 15; start-sleep 5}This results in the following display:
Name PercentProcessorTime IDProcess WSP(MB)
---- -------------------- --------- -------
MicrosoftEdgeCP#3 6 18972 6 WmiPrvSE#3 6 27300 7
svchost#22 0 7316 1Only down side of this is that it fails after one display of information and I don't know why. Any help to this?
UPDATE:3
$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
while ($true) { $tmp = Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | select-object -property Name, @{Name = "CPU"; Expression = {($_.PercentProcessorTime/$cores)}}, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "Memory(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}} | Where-Object {$_.Name -notmatch "^(idle|_total|system)$"} | Sort-Object -Property CPU -Descending| Select-Object -First 15; cls; $tmp | Format-Table -Autosize -Property Name, CPU, PID, "Memory(MB)"; Start-Sleep 5
} Accomplishes all that I am in need of and gives me a starting place in the future to build off of. The issue I had with the table formatter is that format-table must be last.
2 Answers
There is another way to query performance counters via WMI that I think will deliver what you are after. The output also includes the process id which is useful when tracking a process that has multiple instances. It also uses what is called a "calculated property" within select-object to convert the working set value from bytes to megabytes. Also note that the max CPU on a 4 core system is 400, not 100. So to get the overall utilization (that being a max of 100) you need to divide each processes' CPU value by the number of cores.
$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
while ($true) { Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | Where-Object {$_.Name -notmatch "^(idle|_total|system)$" -and $_.PercentProcessorTime -gt 0} | Format-Table -Autosize -Property @{Name = "CPU"; Expression = {[int]($_.PercentProcessorTime/$cores)}}, Name, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "WSP(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}} Start-Sleep 5
}
CPU Name PID WSP(MB)
--- ---- --------- ------- 1 chrome 12476 64 2 wuauclt 7504 0 3 SearchIndexer 10128 22 4 TIworker 11380 102 1 while (1) {cls;
Get-WmiObject Win32_PerfFormattedData_PerfProc_Process |
select-object -property Name, PercentProcessorTime, IDProcess, @{"Name" = "WSP(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}} |
? {$_.Name -notmatch "^(idle|_total|system)$"} |
Sort-Object -Property PercentProcessorTime -Descending|
Select-Object -First 15 |
ft Name, PercentProcessorTime, IDProcess, "WSP(MB)" -AutoSize;
start-sleep 5}
This is what I was looking for. Thank you @Clayton for a great starting point. Here is an explanation for what each part does.
while(1) => always running
cls => clear the current screen
Get-WmiObject => used to get all of the current process information
select-object => only select the columns of data that are wanted
? {$_.Name -notmatch "^(idle|_total|system)$"} => remove the process names that have idle, _total, or system in firs part of the process name
Sort-Object => sort the processes by cpu usage in descending order
Select-Object => select only the first 15 items (top 15 cpu processes)
ft => provide table formatting with column titles and using the whole screen
start-sleep => sleep the loop to running approximately every 5 sec