PowerShell PassThru and OutputMode

14 Sep

So I’m probably a bit late to the party on this one, but as I’m brushing up on my PowerShell tekkers, I came across the -PassThru and -OutputMode parameters, available in PS3.

In short, you can output a query to a GridView, make selections from this GUI and then pass these selections back to PowerShell to perform further actions.

For example:

$UserMailbox = Get-Mailbox  -Filter {RecipientTypeDetails -eq "UserMailbox"} | select displayname | Out-GridView -Title "Dave is Awesome" -OutputMode Single  
Get-Mailbox -identity $UserMailbox.displayname | select displayname,emailaddresses,Primarysmtp*,windowsemail*,Forwarding*

This returns a list of all Office 365 mailboxes. I can then select a mailboxe and run PowerShell based on this. In this case, I just return some more information, but this could just as easily be a Set-Mailbox command or similar.

The following just allows multiple items to be selected, by using the CTRL key:

$UserMailbox = Get-Mailbox  -Filter {RecipientTypeDetails -eq "UserMailbox"} | select displayname | Out-GridView -Title "Dave is Awesome" -PassThru  

foreach ($mbx in $userMailbox){
Get-Mailbox -identity $mbx.displayname | select displayname,emailaddresses,Primarysmtp*,windowsemail*,Forwarding*
}

Pretty awesome.