Powershell - include relevant columns from other cmdlets?

Working with Powershell in Exchange, but I suspect the principle applies in any other Powershell area as well...

Question is: How can I include the OrganizationlUnit property (or any other property for that matter) from the initial "get-mailbox" output to be carried over as a column alongside the output of the piped "get-MailboxStatistics" cmdlet?

I cannot Select "OrganizationalUnit" or other Get-Mailbox properties with the piped "Get-MailboxStatistics" unfortunately.

Here is the code:

get-mailbox -OrganizationalUnit "cafenet.edu/Employees" -ResultSize Unlimited | Get-MailboxStatistics | Sort-object TotalItemSize -descending | Select DisplayName,@{label="TotalItemSize MB";expression={$_.TotalItemSize.Value.ToMB()}}

Open in new window

Who is Participating?
If you want to use it later, out of order of the pipeline, then you have to store the value so that you can retrieve it later (using a variable).  Then you can add a calculated property with Select-Object.  See the following with some pseudo-code.
get-mailbox -OrganizationalUnit "cafenet.edu/Employees" -ResultSize Unlimited | ForEach `
    $prop = $_.someproperty
    $_ | Get-MailboxStatistics | Sort-object TotalItemSize -descending |
     Select DisplayName,@{label="TotalItemSize MB";expression={$_.TotalItemSize.Value.ToMB()}},@{n="SomeProperty";e={$prop}}

Open in new window

garryshapeAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.