Skip to content

Instantly share code, notes, and snippets.

@HollisTech
Last active November 7, 2024 15:32
Show Gist options
  • Save HollisTech/3823db6ec7a829cb6b6dd01290ac0357 to your computer and use it in GitHub Desktop.
Save HollisTech/3823db6ec7a829cb6b6dd01290ac0357 to your computer and use it in GitHub Desktop.
Linux Top for Windows - powershell
function top {
$formatNPM = @{
Label = "NPM(K)"
Alignment = "Right"
Width = 7
Expression = {[long]($_.NPM / 1024)}
}
$formatPM = @{
Label = "PM(M)"
Alignment = "Right"
Width = 8
Expression = {"{0:N2}" -f [float]($_.PM / 1MB)}
}
$formatWS = @{
Label = "WS(M)"
Alignment = "Right"
Width = 10
Expression = {"{0:N2}" -f [float]($_.WS / 1MB)}
}
$formatHandles = @{
Label = "Handles"
Alignment = "Right"
Width = 10
Expression = {$_.Handles}
}
$formatCPU = @{
Label = "CPU(S)"
Alignment = "Right"
Width = 10
Expression = {"{0:N2}" -f [float]($_.CPU)}
}
$formatDelta = @{
Label = 'CPU Delta'
Alignment = "Right"
Width = 10
Expression = { "{0:N4}" -f [float]($_.CpuDelta) }
}
$formatId = @{
Label = "Id"
Alignment = "Right"
Width = 7
Expression = {$_.Id}
}
$formatSI = @{
Label = "SI"
Alignment = "Right"
Width = 3
Expression = {$_.SI}
}
$formatName = @{
Label = "ProcessName"
Alignment = "Left"
Width = 20
Expression = {$_.ProcessName.PadRight(20)}
}
$form = $formatNPM, $formatPM, $formatWS, $formatHandles, $formatCPU, $formatDelta, $formatId, $formatSI, $formatName
if ($host -and $host.UI.SupportsVirtualTerminal) {
$psrl = get-module -Name PSReadLine
if ($psrl) {
Remove-Module $psrl
}
Clear-Host
$lines = $Host.UI.RawUI.WindowSize.Height
$cols = $Host.UI.RawUI.WindowSize.Width
[int]$min = 10
$topCount = ($lines - $min)
if ($lines -le $min) {
$topCount = $lines
}
$sortDesc = 'CPU'
$pp = $ProgressPreference
$ProgressPreference = "SilentlyContinue"
$cpuChange = @{}
try {
while (1) {
$newCpuChange = @{}
$info = get-computerinfo
$pInuse = $info.OsTotalVisibleMemorySize - $info.OsFreePhysicalMemory
$phys = [math]::round(($info.OsTotalVisibleMemorySize/1MB),2)
$physuse = ($pInuse/$info.OsTotalVisibleMemorySize).toString("P")
$cpuAv = (Get-CimInstance -ClassName win32_processor | Measure-Object -Property LoadPercentage -Average).Average
write-host "system: $($info.CsCaption) uptime: $($info.OsUptime) OS version $($info.OsVersion) $($info.OSDisplayVersion)"
write-host "process: $($info.OsNumberOfProcesses) users: $($info.OsNumberOfUsers) Sort: $($sortDesc.PadRight(7)) help: ?"
write-host "cpus: $($info.CsNumberOfLogicalProcessors) load: $($cpuAv)% Physical memory: $phys usage: $physUse"
Get-Process | ForEach-Object {
if ($PID -ne $_.ID) {
$delta = 0.0
if ($cpuChange.ContainsKey($_.Id)) {
$delta = [math]::abs([float]$_.CPU - $cpuChange[$_.Id])
}
Add-Member -InputObject $_ -NotePropertyName "CpuDelta" -NotePropertyValue $delta
$newCpuChange[$_.Id] = [float]$_.CPU
$_
}
} | Sort-Object -desc $sortDesc | Select-Object -first $topCount | format-table $form
$cpuChange = $newCpuChange
if ($host.UI.RawUI.KeyAvailable)
{
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown,IncludeKeyUp")
switch ($key.character) {
'c'{
$sortDesc = 'CPU'
}
'd' {
$sortDesc= "CpuDelta"
}
'h' {
$sortDesc = 'Handles'
}
'n'{
$sortDesc = 'NPM'
}
'p' {
$sortDesc = 'PM'
}
'q'{
Clear-Host
return
}
'w'{
$sortDesc = 'WS'
}
'?' {
Clear-Host
""
""
"keys: cdhnpw?"
"c: sort by total cpu time"
"d: sort by cpu time delta"
"h: sort by open handles"
"n: sort by nonpaged memory usage"
"p: sort by paged memory usage"
"w: sort by working set size"
""
"q: exit top"
"?: help screen"
""
"type any key to continue"
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}
}
else {
Start-Sleep 0.5
}
if (($Host.UI.RawUI.WindowSize.Height -ne $lines) -or
($Host.UI.RawUI.WindowSize.Width -ne $cols)) {
Clear-Host
$lines = $Host.UI.RawUI.WindowSize.Height
$cols = $Host.UI.RawUI.WindowSize.Width
$topCount = ($lines - $min)
if ($lines -le $min) {
$topCount = $lines
}
}
$host.UI.RawUI.CursorPosition = @{x = 0; y = 0}
}
}
finally {
$ProgressPreference = $pp
if ($psrl) {
import-Module $psrl
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment