Give me Some Hashtable trick

Welcome Forums General PowerShell Q&A Give me Some Hashtable trick

This topic contains 4 replies, has 5 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #111839

    Participant
    Points: 0
    Rank: Member

    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.

  • #111944

    Participant
    Points: 0
    Rank: Member

    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
    
    
  • #111946

    Keymaster
    Points: 1
    Rank: Member

    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.

  • #111964

    Participant
    Points: 0
    Rank: Member

    Try this 🙂

    $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++
    }
    
  • #111971

    Participant
    Points: 0
    Rank: Member

    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

You must be logged in to reply to this topic.