In PowerShell, how can I combine the results of two commands that have a 1-to-1 relashionship?

Viewed 60994

This particular example is Get-User and Get-Mailbox (Exchange 2010). Get-User returns some of the columns I need, and Get-Mailbox some others. I am having difficulty figuring out how I can combine the results of the two into a single table with the results from both.

Get-User -Filter "..." | Get-Mailbox -Filter "..."

How do I take the results of a command similar to the above and turn it into results similar to below?

FirstName  LastName  Alias   CustomAttribute1
---------  --------  ------  ----------------
Bob        Smith     bsmith  Example
Johnny     NoMail
Adam       Blye      ablye   Has a Mailbox

Note that FirstName and LastName are not returned by Get-Mailbox, and conversely Alias and CustomAttributes are not returned from Get-User. Not every user has a mailbox, so sometimes a portion of the columns would be null. But I'm having a devil of a time figuring out the best way to return a combined table like this.

4 Answers

One liner to get FirstName, LastName, Alias and CustomAttribute1:

Get-User | Select Firstname, Lastname, @{Name="Alias"; Expression={(Get-Mailbox $_.Name).Alias}}, @{Name="CustomAttribute1"; Expression={(Get-Mailbox $_.Name).CustomAttribute1}}
Related