Colin
Colin

Reputation: 2743

Processing the values of a grouping independently

How do you process the values of a group independently?

For example, I have the following grouping:

$grouping = Get-ChildItem -Recurse | Group-Object Extension

I'd like to perform some operation on each set of the grouped child items:

foreach ($group in $grouping}
{ 
    $group.Values | Measure-Object
}

(Measure-Object is just a stand-in for an actual operation. I know getting the count can be done via $grouping directly.)

What I'm expecting is something like:

Count    : 96
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

Count    : 12
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

Count    : 14
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

What I get is:

Count    : 122
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

The Measure-Object cmdlet is being instantiated once for the entire pipeline, not once per set of values.

How do I instantiate it once per set of values?

20150220 EDIT:

I've posed the actual problem as a separate, but much more specific question.

Upvotes: 0

Views: 74

Answers (2)

Matt
Matt

Reputation: 46710

I got the same output that @BaconBits reported with your code. The way I was able to get you desired output was to use the following:

foreach ($group in $grouping)
{ 
    $group.Group | Measure-Object
}

But that only addresses your provided sample and potentially not your actual issue.

If that is still not working for you you need to be more explicit with you issue and sample if possible.

Upvotes: 1

Bacon Bits
Bacon Bits

Reputation: 32170

When I run the code, I get:

Count    : 1
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

But I get that once for every extension found.

I'm not sure what you're trying to do, but when I run this $group.Values is a System.Collections.ArrayList but only contains one value.

So $grouping.Values displays (for example):

.pdf
.txt
.txtfoo
.ps1
<etc.>

And $grouping[0].Values, which is what $group.Values would be for the first iteration, displays:

.pdf

And that's it.

My guess is you either want foreach ($group in $Grouping.Values) to iterate through each extension, or like @Matt says you should refer to $group.Group to get a the collection of items in that grouping. I have no idea which since I have no idea what you're actually trying to do.

Upvotes: 0

Related Questions