Skip to content

Instantly share code, notes, and snippets.

@HollisTech
Last active February 10, 2023 16:53
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 {
if ($host -and $host.UI.SupportsVirtualTerminal) {
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"
try {
while (1) {
$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))"
write-host "cpus: $($info.CsNumberOfLogicalProcessors) load: $($cpuAv)% Physical memory: $phys usage: $physUse"
Get-Process | Sort-Object -desc $sortDesc | Select-Object -first $topCount | format-table
# if ($host.UI.RawUI.KeyAvailable) # doesn't work?
if ([Console]::KeyAvailable)
{
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
switch ($key.character) {
'c'{
$sortDesc = 'CPU'
}
'h' {
$sortDesc = 'Handles'
}
'n'{
$sortDesc = 'NPM'
}
'p' {
$sortDesc = 'PM'
}
'q'{
Clear-Host
return
}
'w'{
$sortDesc = 'WS'
}
}
}
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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment