Overriding Out-Default (Part 2)

by Jul 5, 2019

When you override Out-Default to do something meaningful, you really want to make sure the old behavior isn’t lost, and instead just something new is added. Here is an example that uses the concept of “Proxy Functions”.

The original input is forwarded (proxied) to the original Out-Default cmdlet. In addition, the function opens its own private Out-GridView window and echoes the output to this window as well.

function Out-Default
{
    param(
        [switch]
        $Transcript,

        [Parameter(ValueFromPipeline=$true)]
        [psobject]
        $InputObject
    )

    begin
    {
        $pipeline = { Microsoft.PowerShell.Core\Out-Default @PSBoundParameters }.GetSteppablePipeline($myInvocation.CommandOrigin)
        $pipeline.Begin($PSCmdlet)

        $grid = { Out-GridView -Title 'Results' }.GetSteppablePipeline()
        $grid.Begin($true)
    
    }

    process
    {
        $pipeline.Process($_)
        $grid.Process($_)
    }

    end
    {
        $pipeline.End()
        $grid.End()
    }

} 

To remove the override, simply run:

 
PS C:\> del function:Out-Default
 

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!