Save the following in a file called mytop.ps1
in a folder that is in your PATH
environment variable. Then use one of the following from any PowerShell console:
mytop
- to use default sort by the 'Memory' column and show the first 30 lines.
mytop CPU 50
- to sort by the 'CPU' column and show the first 50 lines.
While(1) {$p = myTop Memory 50; cls; $p}
- to have it refresh every second or so.
mytop.ps1
contents:
##################################################
# Linux top equivalent in PowerShell
##################################################
if ($args[0] -eq $null) {
$SortCol = "Memory"
} else {
$SortCol = $args[0]
}
if ($args[1] -eq $null) {
$Top = 30
} else {
$Top = $args[1]
}
$LogicalProcessors = (Get-WmiObject -class Win32_processor `
-Property NumberOfLogicalProcessors).NumberOfLogicalProcessors;
function myTopFunc ([string]$SortCol = "Memory", [int]$Top = 30) {
## Check user level of PowerShell
if (
([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
)
{
$procTbl = get-process -IncludeUserName | select ID, Name, UserName, Description, MainWindowTitle
} else {
$procTbl = get-process | select ID, Name, Description, MainWindowTitle
}
Get-Counter `
'\Process(*)\ID Process',`
'\Process(*)\% Processor Time',`
'\Process(*)\Working Set - Private'`
-ea SilentlyContinue |
foreach CounterSamples |
where InstanceName -notin "_total","memory compression" |
group { $_.Path.Split("\\")[3] } |
foreach {
$procIndex = [array]::indexof($procTbl.ID, [Int32]$_.Group[0].CookedValue)
[pscustomobject]@{
Name = $_.Group[0].InstanceName;
ID = $_.Group[0].CookedValue;
User = $procTbl.UserName[$procIndex]
CPU = if($_.Group[0].InstanceName -eq "idle") {
$_.Group[1].CookedValue / $LogicalProcessors
} else {
$_.Group[1].CookedValue
};
Memory = $_.Group[2].CookedValue / 1KB;
Description = $procTbl.Description[$procIndex];
Title = $procTbl.MainWindowTitle[$procIndex];
}
} |
sort -des $SortCol |
select -f $Top @(
"Name", "ID", "User",
@{ n = "CPU"; e = { ("{0:N1}%" -f $_.CPU) } },
@{ n = "Memory"; e = { ("{0:N0} K" -f $_.Memory) } },
"Description", "Title"
) | ft -a
}
myTopFunc -SortCol $SortCol -top $Top
Example output:
Name ID User CPU Memory Description
---- -- ---- --- ------ -----------
sqlservr 7776 NT SERVICE\MSSQLSERVER 0.0% 19,001,488 K SQL Server Windows NT - 64 Bit
python 12872 NA\user1 0.0% 2,159,796 K Python
svchost 3328 NT AUTHORITY\SYSTEM 1.6% 1,022,080 K Host Process for Windows Services
onedrive 11872 NA\user1 0.0% 423,396 K Microsoft OneDrive
python 13764 NA\user1 0.0% 304,608 K Python
chrome 21188 NA\user1 0.0% 250,624 K Google Chrome
python 28144 NA\user2 0.0% 225,824 K Python
code 21384 NA\user1 0.0% 211,160 K Visual Studio Code
code 27412 NA\user2 0.0% 185,892 K Visual Studio Code
ssms 18288 NA\user1 29.5% 155,452 K SSMS
chrome 7536 NA\user1 0.0% 154,124 K Google Chrome
code 21652 NA\user1 0.0% 149,900 K Visual Studio Code
explorer 3204 NA\user1 0.0% 134,340 K Windows Explorer
python 11712 NA\user1 0.0% 130,624 K Python
chrome 21588 NA\user1 0.0% 107,448 K Google Chrome
code 10152 NA\user1 0.0% 100,880 K Visual Studio Code
code 20232 NA\user2 0.0% 99,124 K Visual Studio Code
python 22184 NA\user1 0.0% 94,800 K Python
code 14828 NA\user1 0.0% 84,872 K Visual Studio Code
searchui 13344 NA\user1 0.0% 78,260 K Search and Cortana application
com.docker.service 10644 NT AUTHORITY\SYSTEM 0.0% 77,332 K Docker.Service
Additional credit to:
- rokumaru for https://stackoverflow.com/a/55698377/5060792
- LotPings for https://stackoverflow.com/a/55680398/5060792
- DBADon for https://stackoverflow.com/a/55697007/5060792
This falls squarely in the http://superuser.com category of questions.
– None – 2010-08-15T01:26:46.677Cool -didnt realize that site even existed! (I am primarily a C# developer) – None – 2010-08-15T02:37:29.380
3The CPU property on the Process Object is not CPU percentage it is CPU time total since process start. – None – 2013-04-02T23:07:16.800