# Linux “Top” command for Windows Powershell?

I am looking for a PowerShell cmdlet that can provide similar functionality to the Linux Top app. Something that refreshes at some given interval and displays the process list with CPU % util.

I have seen scripts that list CPU % utilization in a loop but something like top would be way more handy as we have setup SSH/Powershell access for management (I still prefer a putty shell!)

-

## migrated from stackoverflow.comAug 17 '10 at 0:43

This question came from our site for professional and enthusiast programmers.

This falls squarely in the superuser.com category of questions. –  Gabe Aug 15 '10 at 1:26
Cool -didnt realize that site even existed! (I am primarily a C# developer) –  TimAtVenturality Aug 15 '10 at 2:37
The CPU property on the Process Object is not CPU percentage it is CPU time total since process start. –  user213525 Apr 2 '13 at 23:07

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

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

-
close enough - I can tweak it from here... well, done! (I am a C# developer, but manage our servers too - so coming up the PowerShell curve...) –  TimAtVenturality Aug 15 '10 at 2:35
if you want to learn more - by example - check out www.poshcode.org –  x0n Aug 15 '10 at 16:35
@TimAtVenturality - You can wrap the script as a function with parameters to more closely replicate top. –  Joe Internet Aug 17 '10 at 1:56
While(1) {ps | sort -des cpu | select -f 15 | ft -a; sleep 1; cls}


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

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.

Here is a breakdown of the shortened commands used

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

I'm not aware of a PowerShell cmdlet that provides the functionality. There is a freeware external command that does about what you want. Look at Mark Russinovich's pslist from the Sysinternals suite. Pslist provides a list of executing processes in a configurable view. "pslist -s" provides the sort of continuous update you want, with a default refresh rate of once per second.

I prefer to use Mark's GUI Process Explorer, but pslist is handy for console sessions.

Dennis

-
while (1) {ps | sort -desc cpu | select -first 30;
sleep -seconds 2; cls;
write-host "Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName";
write-host "-------  ------    -----      ----- -----   ------     -- -----------"}


This is just a slightly nicer way, as you get to see the headings up top every time

-

Provides the nice headings at the top with every update without needing to clear the entire console.

$saveY = [console]::CursorTop$saveX = [console]::CursorLeft

while ($true) { Get-Process | Sort -Descending CPU | Select -First 30; Sleep -Seconds 2; [console]::setcursorposition($saveX,\$saveY+3)
}

-

Also, I want to point out that if you would like a linux-like enviroment for Windows you can use Cygwin. It brings the Linux Enviroment to Windows. You can use almost every command. Not sure how useful this is to you though.

http://www.cygwin.com/

-

To run top directly from cmd you'll need to create file %WINDIR%\top.bat with this code:

@echo off && cls && @echo TOP Program initialisation. Please Wait...
powershell -ExecutionPolicy unrestricted -command "& {cls; While(1) {ps | sort -des cpu | select -f 35 | ft -a; sleep 2; cls}}"

-