Powershell pipeline - Retrieve outputs from first cmdlet?

powershell

Solution

Start with the execution of one cmdlet, pipe the results to `Foreach-Object` and then save a reference to the current object ($user), now execute the second command and save it in a variable as well. Create new object with properties from both objects.

You also need to filter users that have mailboxes, use the RecipientTypeDetails parameter.

$users = Get-User -RecipientTypeDetails UserMailox 
$users | Foreach-Object{

    $user = $_
    $stats = Get-MailboxStatistics $user

    New-Object -TypeName PSObject -Property @{
        FirstName = $user.FirstName
        LastName = $user.LastName
        MailboxSize = $stats.TotalItemSize
        ItemCount =  $stats.ItemCount   
    }
}

Problem

I am trying a few things in Powershell and what I don't manage to achieve is the following (in Exchange): ``` Get-User | Get-MailboxStatistics ``` But in the output I would like some fields/outputs from the `"Get-User"` cmdlet and some fields/outputs from the `"Get-MailboxStatistics"` cmdlet. If anyone has an answer, I have searched the web but with no success as I've had difficulties explaining it in a few words. Thanks in advance for your help.

Original source