Linux "Top" command for Windows Powershell?

This is a simple one-liner that will also keep the labels at the top:

While(1) {ps | sort -des cpu | select -f 15 | ft -a; sleep 1; cls}

This works because formatting the table without any parameters just drawls the default table. autosize is used to automatically adjust the column width so all the data can fit on screen.


Breakdown of the shortened commands:

  • select -f: shortcut for -first
  • ft: shortcut for Format-Table
  • -a: shortcut for -autosize
  • sleep: defaults to using seconds

There's nothing that I know of that in single cmdlet form, but like you say, scripts are easy to write to emulate top.

while (1) { ps | sort -desc cpu | select -first 30; sleep -seconds 2; cls }

A similar solution as others, but using Get-Counter instead of Get-Process:

While(1) {  $p = get-counter '\Process(*)\% Processor Time'; cls; $p.CounterSamples | sort -des CookedValue | select -f 15 | ft -a}

Sample output:

Path                                                      InstanceName              CookedValue
----                                                      ------------              -----------
\\server_name\process(_total)\% processor time                 _total               4806.03969127454
\\server_name\process(idle)\% processor time                   idle                 1103.7573538257
\\server_name\process(program2)\% processor time               program              749.692930701698
\\server_name\process(program5)\% processor time               program              563.424255927765
\\server_name\process(program1)\% processor time               program              535.714866291973
\\server_name\process(program6)\% processor time               program              455.665518455242
\\server_name\process(program3)\% processor time               program              426.416718284128
\\server_name\process(program)\% processor time                program              395.628507577693
\\server_name\process(program4)\% processor time               program              335.591496700144
\\server_name\process(microsoftedgecp2)\% processor time       microsoftedgecp      129.310484967028
\\server_name\process(system)\% processor time                 system               80.0493478367316
\\server_name\process(chrome8)\% processor time                chrome               1.53941053532176

I found most of the other solutions here using Get-Process report the total CPU time since the start of the process, which wasn't useful on my server that stays up 24/7 where the top result was always svchost and system at millions of seconds.

  • A true top or Task Manager equivalent would give a snapshot of the CPU usage recorded recently over some fixed time and Get-Counter provides that. I figured this alternative is worth contributing since this question is still the top Google result for powershell top.

Based on Example 13 from the Get-Counter docs, a breakdown of the command:

  • While(1) {: Creates a loop
  • get-counter '\Process(*)\% Processor Time': Selects CPU % data, which takes a significant amount of time to return, so no need to sleep

  • cls: Clear for the new table

  • sort -des CookedValue: Sort largest on top for CookedValue [field we're interested in]
  • select -f 15: Display first 15
  • ft -a: Display in formatted table