Give me Some Hashtable trick

i’m new to PowerShell, just want to know if there is a possibility in generating a new PropertyName with its own values? Take a look below for gps cmndlet:

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
201 7 2288 14080 149 0.31 4752 ApplicationFrameHost
165 6 7376 10728 89 4.70 3380 audiodg
115 5 1212 6224 115 0.02 152 chrome
378 25 103220 98708 954 635.25 872 chrome
232 11 11044 19172 742 0.22 2264 chrome
392 16 72640 79920 454 361.09 2432 chrome
369 19 52644 79748 855 15.75 3036 chrome
1178 32 50748 91280 401 227.72 3912 chrome

I want to make a NewProperty name to be called “RANK”, and it will rank the Handle property 1st to 8th, where it will be based on Handles’value. For example Handles = 1178 would be the 1st and Handles = 165 would be the 8th. How can I do that?

Thank You Very Much guys.

Are you looking for something like this?

Get-Process | Select-Object -First 8 Name,CPU,ID,

Handles,@{n='Rank';exp={$_.Handles}} |

Sort-Object -Property Rank -Descending | Format-Table

Because you’d basically have to sort the items first, this would probably be best accomplished by creating a standalone function, “Select-Process,” for example, that did this. I’ll give you a very very high level example.

function Select-Process {

[CmdletBinding()]

Param(

[Parameter(ValueFromPipeline=$True)]

[object]$InputObject

)

BEGIN { $rank = 0 }

PROCESS {

$InputObject | Select *,@{n='Rank';e={$x}}

$x++

}

}

 

Get-Process |

Sort-Object -Prop Handles |

Select-Process

 

Something vaguely along those lines. There are probably a dozen ways to do it - this is just to give you a rough idea. Hope it helps.

Try this :slight_smile:

$gps = gps | sort handles | select -First 8
$i = 1
foreach($r in $gps){
    New-Object PSObject @{
        Rank = $i
        Handles = $r.Handles
        NPM     = $r.NPM
        PM      = $r.PM
        WS      = $r.WS
        CPU     = $r.CPU
        Id      = $r.Id
        SI      = $r.SI
        ProcessName = $r.ProcessName
    }
    $i++
}

Please try this…

Get-Process | Sort-Object handles -Descending | select -First 8 @{Name=‘Rank’;expression={$_.Handles}},ProcessName,Id,cpu,si,ws,pm,npm | ft -AutoSize