I have a custom object with several NoteProperty values…pretty standard. I want to group these values on an identifier value, so I use “Group-Object”. Here is a sample:
[PSCustomObject]@{
ID = 'NO0001'
Path = '\\server\share\path\folder'
DataCount = 4
DataCompleteCount = 2
}
I get an array of these back from a routine, and there can be multiple Path/DataCount/DataCompleteCount sets for each individual ID. I want to display a 3 column table like so:
ID DataCount DataCompleteCount
NO0001 12 8
NO0002 14 9
...
I tried doing this:
PS C:\Users> $ResultArray | Group ID
Count Name Group
----- ---- -----
3 NO0001 {@{ID=NO0001; Path=\\server\share\path\folder; DataCount=4; DataC...
1 NO0002 {@{ID=NO0002; Path=\\server2\share2\path\folder; DataCount=2; Dat...
...
But no matter what I tried in a select statement I couldn’t get at the embedded objects in the returned group field. Suggestions?
Grouped objects are under .Group collection:
$obj = @()
$obj += [PSCustomObject]@{
ID = 'NO0001'
Path = '\\server\share1\path\folder'
DataCount = 4
DataCompleteCount = 2
}
$obj += [PSCustomObject]@{
ID = 'NO0001'
Path = '\\server\share2\path\folder'
DataCount = 6
DataCompleteCount = 4
}
$obj += [PSCustomObject]@{
ID = 'NO0002'
Path = '\\server\share1\path\folder'
DataCount = 9
DataCompleteCount = 8
}
$obj += [PSCustomObject]@{
ID = 'NO0002'
Path = '\\server\share2\path\folder'
DataCount = 4
DataCompleteCount = 3
}
$grp = $obj | Group-Object -Property ID
$grp | Select Count, Name, @{Name='TotalDataCount';Expression={($_.Group.DataCount | Measure-Object -Sum).Sum}}
Output:
Count Name TotalDataCount
----- ---- --------------
2 NO0001 10
2 NO0002 13
Thank you, Rob. I had everything right except how I was formulating the calculated expressions. In some statements you use @{Name=‘text’;Value={Code}} and in others you use @{Name=‘text’;Expression={Code}}. I was using “Value” instead of “Expression”, which didn’t work at all. Your example is very well written and easy to understand.