frodo
frodo

Reputation: 1063

How to get a numbered count of different items in powershell

Im using the following powershell query to get a list of disks:

Get-NcDisk | Select-Object -Property  model | Sort-Object -Property Model -Descending | foreach {$_.model}

It outputs like below:

X316_SMKRE06TA07
X316_HARIH06TA07
X316_HARIH06TA07
X316_HARIH06TA07

How can I get it to output a numbered count of each type of disk like below:

1 X316_SMKRE06TA07
3 X316_HARIH06TA07

Upvotes: 1

Views: 196

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13217

Group-Object will do this for you..

I can't use Get-NcDisk but it may just be:

Get-NcDisk | Select-Object -ExpandProperty model | Group-Object

Example output using a string array:

"X316_SMKRE06TA07","X316_HARIH06TA07","X316_HARIH06TA07","X316_HARIH06TA07" | Group-Object

Count Name                      Group                                                         
----- ----                      -----                                                         
    1 X316_SMKRE06TA07          {X316_SMKRE06TA07}                                            
    3 X316_HARIH06TA07          {X316_HARIH06TA07, X316_HARIH06TA07, X316_HARIH06TA07}        

Upvotes: 4

Related Questions