Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Get-unique items from an array

$
0
0

Try Sort-Object -Unique since it lets you indicate the property that you want to sort\be unique:

$process = Get-Process
$cpu = $process | Sort-Object CPU -Descending | Select-Object -First 5
$ws  = $process | Sort-Object WorkingSet -Descending | Select-Object -First 5
 
$a = @()
$a += $cpu
$a += $ws
 
"Total object Count: {0}" -f $a.Count
$a
 
"Total unique ID's: {0}" -f ($a | Select ID -Unique).Count
$a | Select ID -Unique | Format-Table -AutoSize
 
$unique = $a | sort-object id -Unique
"Total with Sort-Object -Unique:  {0}" -f $unique.Count 
$unique

Output:

Total object Count: 10
 
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                       
——-  ——    —–      —– —–   ——     — ———–                                                       
   3990     116   155344     183300   867 2,941.16   4124 OUTLOOK                                                           
   1529     135   152700     150592   594 2,420.08   3164 chrome                                                            
    221      59   180724     109000   358 2,208.63   2060 chrome                                                            
    564      15     4820       3520   107 1,258.55   2732 SynTPEnh                                                          
    428      32   143480     123344   460 1,190.34   3240 chrome                                                            
   1232     126   364880     313972  1147   955.64   6024 powershell_ise                                                    
   3990     116   155344     183300   867 2,941.16   4124 OUTLOOK                                                           
   3243     252   118944     162424   955   976.08   2692 explorer                                                          
   1529     135   152700     150592   594 2,420.08   3164 chrome                                                            
   2508      75   120028     130288   727   162.45   3368 lync                                                              
Total unique ID's: 8
 
 
 
  Id
  –
4124
3164
2060
2732
3240
6024
2692
3368
 
 
Total with Sort-Object -Unique:  8
 
Handles NPM(K)  PM(K)  WS(K) VM(M)   CPU(s)   Id ProcessName   
——- ——  —–  —– —–   ——   — ———–   
    221     59 180724 109000   358 2,208.63 2060 chrome        
   3243    252 118944 162424   955   976.08 2692 explorer      
    564     15   4820   3520   107 1,258.55 2732 SynTPEnh      
   1529    135 152700 150592   594 2,420.08 3164 chrome        
    428     32 143480 123344   460 1,190.34 3240 chrome        
   2508     75 120028 130288   727   162.45 3368 lync          
   3990    116 155344 183300   867 2,941.16 4124 OUTLOOK       
   1232    126 364880 313972  1147   955.69 6024 powershell_ise

Reference: http://blogs.technet.com/b/heyscriptingguy/archive/2012/01/15/use-powershell-to-choose-unique-objects-from-a-sorted-list.aspx


Viewing all articles
Browse latest Browse all 13067

Trending Articles