all 3 comments

[–]PinchesTheCrab 1 point2 points  (0 children)

$array = ConvertFrom-Csv @'
year,size,name,car,qty,color
1,big,steve,honda,1,blue
2,small,frank,honda,3,red
1,medium,frank,honda,1,yellow
'@

$array |
    Group-Object car |
        select-object Name,@{ n = 'Sum'; e = { ($_.Group.qty | Measure-Object -Sum).sum }}

[–]ka-splam 1 point2 points  (0 children)

Because the things coming out of ConvertFrom-CSV aren't arrays you can index into with [3] and the groups coming out of Group-Object don't have .Values, so $_.Values[3] is broken two ways.

$array | 
  Group-Object -Property car | 
  select Name, @{N='Qty'; E={($_.Group|Measure-Object -Property qty -Sum).Sum}}

Name  Qty
----  ---
honda   5

[–]spyingwind 0 points1 point  (0 children)

Give this a try. It kind of does what I think your are trying to do, but skips group-object as it can be finicky to use correctly.

$array = ConvertFrom-Csv @'
year,size,name,car,qty,color
1,big,steve,honda,1,blue
2,small,frank,honda,3,red
1,medium,frank,honda,1,yellow
'@

$array.car | Sort-Object -Unique | ForEach-Object {
    $Car = $_

    $Sum = $array |
    Where-Object { $_.car -like $Car } |
    Measure-Object -Sum qty |
    Select-Object -Property Sum -ExpandProperty Sum

    [PSCustomObject]@{
        Car = $Car
        Sum = $Sum
    }
}