Get value from previous Cmdlet in the pipeline

Question:

I got the following command:
Get-Mailbox | Get-MailboxPermission | Select-Object Identity,User,AccessRights | Format-Table -AutoSize.
I want to be able to get the PrimarySMTPAddress value from the previous pipe where I got the results for the Get-Mailbox.
At the moment when I add the property PrimarySMTPAddress I receive nothing in the column.

The final result should like this:

Answer:

You can use the ForEach-Object cmdlet, assign the value to a variable, then use Write-Output to send the pipeline value to the next cmdlet. When you use Select-Object, you can access the variable value with a calculated property.

Source:

Get value from previous Cmdlet in the pipeline by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply