Group-object

  • This should be simple, but I'm having trouble with suming values across groups:

    $a = @()
    $b = New-Object pscustomobject -property @{TypeName = "Type A"; TypeId = "12"; Calls = 43}
    $a += $b
    $b = New-Object pscustomobject -property @{TypeName = "Type A"; TypeId = "12"; Calls = 12}
    $a += $b
    $b = New-Object pscustomobject -property @{TypeName = "Type B"; TypeId = "34"; Calls = 11}
    $a += $b
    $b = New-Object pscustomobject -property @{TypeName = "Type B"; TypeId = "34"; Calls = 17}
    $a += $b

    $a gives
    TypeId TypeName  Calls
    ------ -----    -----
    12     Type A    43
    12    Type A    12
    34    Type B    11
    34    Type B    17

    I want to group by TypeId,
    TypeName to get

    TypeId TypeName Calls
    ------ ----- -----
    12  Type A    55
    34  Type B    28

    When I do

    $Data2 = $Data | Group-Object -Property TypeName, TypeId|
            %{New-Object psobject -Property @{
                                                Item = $_.Name
                                                nCalls = ($_.Group | Measure-Object nCalls -Sum).Sum
                                                }
            }

    i get
    Item          nCalls
    12, Type A    55
    34, Type B    28

    How can I get the original properties as columns?

  • Found the solution here: https://stackoverflow.com/questions/30769809/powershell-group-by-multiple-properties
    Clunky...

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply