Help with Group

I have a CSV with:

Name, Quantity, Color
Item1,1,Black
Item2,1,White
Item1,2,Black

I would like to Group by name and color but also have the Count multiplied by the quantity to give me a Total. All works fine if the Quantity is always 1 on each one but when I had a quantity of 2 on the last one it does not account for that.

The output I am looking for is:

Item1,3,Black
Item2,1,White

But what I get is:

Item1,2,Black
Item2,1,White

This is what I am using to test (and learn :smile: )

$Test = import-csv C:\Logs\Test.csv

$Testing = $Test | Group-Object Name |
	Foreach-Object {
		[pscustomobject]@{
			Name	 = $_.Group[0].Name
            Quantity = $_.Group[0].Quantity
            Count	 = $_.Count
            Total	 = ($_.Group[0].Quantity | Measure -Sum).Sum * ($_.Count | Measure -Sum).Sum
			Color    = $_.Group[0].Color

		}
	}
$Testing | ft

I am obviously a rookie at this but trying to learn as I go along. I know I probably have way to much code for what I need but using it to understand and how everything works.

Thanks,
Scott

Hi, welcome back :wave:

Pretty sure you don’t want to multiply. If you multiply count by quantity you’ll end up with (2x1) + (2x2) which is 6.

A simple sum is all you need:

$csv = @"
Name, Quantity, Color
Item1,1,Black
Item2,1,White
Item1,2,Black
"@

$test = ConvertFrom-Csv $csv

$test | Group-Object -Property Name | ForEach-Object {
    [PSCustomObject]@{
        Name   = $_.Name
        Total  = ($_.Group.Quantity | Measure-Object -Sum).Sum
        Color  = $_.Group[0].Color
    }
}

Thanks! I knew I was overcomplicating it. That’s seems to work in testing.

Thanks again!

Scott