Group and Sum CSV with unknown columns.

Hi,

Wondering if someone would be able to help me. I’m trying to Import , Group, Sum and then Export a CSV. The problem is that my CSV has a unknown number of columns of the following format.

GroupA,GroupB,GroupC,ValueA,ValueB,ValueC,ValueD…

GroupA, B and C are constant and the fields I want to group by - I know the names of these fields in advance. The problem is there are an unknown number of Value columns - all of which I want to Sum (and don’t know the names of in advance.)

I’m comfortable getting this code working if I know the name of the Value fields and have a fixed number of Value Fields. But I’m struggling to get code for unknown names and number of columns.

 

Any help would be appreciated,

 

Sam

 

 

May be something like this. (provided the GroupA,GroupB etc are column headers)

Import-Csv -Path C:\CsvPath.csv | Get-Member -MemberType Properties | Where-Object -FilterScript {$_.Name -notmatch "GroupA|GroupB|GroupC"} | Select-Object -ExpandProperty Name

I believe `Import-Csv` may not be helpful in this case, try the snippet below, hopefully it works for you…

https://gist.github.com/kpatnayakuni/dab0f1c99d34fc18f869451d93efe76e