Skip to main content
Added correct markdown for weblinks ; Grammatical corrections
Source Link
JW0914
  • 8.3k
  • 7
  • 31
  • 50

Save the following in a file calledto mytop.ps1 in, within a folder that is in your PATH environment variable. Then, then use one of the following from any PowerShell console:

  1. mytop - to use default sort by the 'Memory' column and show the first 30 lines.
  2. mytop CPU 50 - to sort by the 'CPU' column and show the first 50 lines.
  3. While(1) {$p = myTop Memory 50; cls; $p} - to have it refresh every second or so.

mytop.ps1 contents:

  • mytop: [Defautl] Sort by Memory and show the first 30 lines
  • mytop CPU 50: Sort by 'CPU' and show the first 50 lines.
  • While(1) {$p = myTop Memory 50; cls; $p}: Refreshes every second
##################################################
#  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
##################################################
#  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
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:

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
  1. rokumaru for https://stackoverflow.com/a/55698377/5060792
  2. LotPings for https://stackoverflow.com/a/55680398/5060792
  3. DBADon for https://stackoverflow.com/a/55697007/5060792

Additional credit to answers from rokumaru, LotPings, and DBADon

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:

  1. mytop - to use default sort by the 'Memory' column and show the first 30 lines.
  2. mytop CPU 50 - to sort by the 'CPU' column and show the first 50 lines.
  3. 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
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:

  1. rokumaru for https://stackoverflow.com/a/55698377/5060792
  2. LotPings for https://stackoverflow.com/a/55680398/5060792
  3. DBADon for https://stackoverflow.com/a/55697007/5060792

Save the following to mytop.ps1, within a folder in your PATH environment variable, then use one of the following from any PowerShell console:

  • mytop: [Defautl] Sort by Memory and show the first 30 lines
  • mytop CPU 50: Sort by 'CPU' and show the first 50 lines.
  • While(1) {$p = myTop Memory 50; cls; $p}: Refreshes every second
##################################################
#  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
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 answers from rokumaru, LotPings, and DBADon

fixed CPU if condition
Source Link
Clay
  • 571
  • 3
  • 8
  • 18
##################################################
#  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($_.NameGroup[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
##################################################
#  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($_.Name -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
##################################################
#  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
added 25 characters in body
Source Link
Clay
  • 571
  • 3
  • 8
  • 18

mytop.ps1 contents:

mytop.ps1 contents:

Source Link
Clay
  • 571
  • 3
  • 8
  • 18
Loading