Search code examples
powershellpowershell-2.0exchange-server-2010

How do I access a "Group" member after I run PS "Group-Object"


I have the sourcedata similar to the following denormalized table

DisplayName                  LogRecordCount LogRecordBytes
-----------                  -------------- --------------
Mailbox - Low,                    459          43756
Mailbox - Low,                    1628         185542
Mailbox - Low,                    2575         264474
Mailbox - Low,                     522          48813
Mailbox - Low,                     410         138212
Mailbox - Low,                    1057         200043
Mailbox - Freed                  3866        2170719
Mailbox - Freed                   606         370304
Mailbox - Freed                  4137        1939924
Mailbox - Freed                 3655        1654650

My goal is to write a summary similar to the following

DisplayName                  LogRecordCount  
-----------                  -------------- 
Mailbox - Low,                     6651            
Mailbox - Freed                   12264     

The following code is very close to getting what I need, but the "DisplayName" isn't appearing. The value of DisplayName is buried in the result of the Group-Object command.

$stats | ? {$_.DigestCategory -eq 'LogBytes'} | group MailboxGuid | %{
    New-Object psobject -Property @{
        MailboxGuid = $_.Name
        LogRecordBytes = ($_.Group | Measure-Object LogRecordBytes -Sum).Sum
        DisplayName   = $_.Group.DisplayName
    }
}| sort-object LogRecordBytes |  ft -a DisplayName, MailboxGuid, LogRecordBytes

Question

What is the proper syntax for DisplayName = $_.Group.DisplayName to display the display name in the results?


Solution

  • Swap out your DisplayName = .. line with the following:

    DisplayName = $_.Group | Select-Object -ExpandProperty DisplayName -Unique
    

    If for some magical reason there are different DisplayName values in the group, you will get an array like {value1, value2}, but normally it will return a single name only :)