When you output data in PowerShell, it gets silently piped to Out-Default and ends up as text in the console. By overriding Out-Default, you can change this behavior and for example send all PowerShell output to a grid view window. You can in fact even separate regular output from error messages, and display both in separate windows.

Here are two functions: Enable-GridOutput and Disable-GridOutput. When you run Enable-GridOutput, it overrides Out-Default and sends regular output to a “Output” grid view window, and converts error messages into useful text which is output in a separate “Error” grid view window.

When you run Disable-GridOutput, the override is removed, and you return to default behavior:

function Enable-GridOutput
{
    function global:Out-Default
    {
        param
        (
            [Parameter(ValueFromPipeline=$true)][Object]
            $InputObject
        )

        begin
        {
            $cmd = $ExecutionContext.InvokeCommand.
                    GetCommand('Microsoft.PowerShell.Utility\Out-GridView',
                    [Management.Automation.CommandTypes]::Cmdlet)
                
            $p1 = {& $cmd -Title 'Output'  }.
                    GetSteppablePipeline($myInvocation.CommandOrigin)
            $p2 = {& $cmd -Title 'Error'  }.
                    GetSteppablePipeline($myInvocation.CommandOrigin)
            
            $p1.Begin($PSCmdlet)
            $p2.Begin($PSCmdlet)    
        }

        process
        {
            if ($_ -is [Management.Automation.ErrorRecord])
            {
                $info = $_ | ForEach-Object { [PSCustomObject]@{
                        Exception = $_.Exception.Message
                        Reason    = $_.CategoryInfo.Reason
                        Target    = $_.CategoryInfo.TargetName
                        Script    = $_.InvocationInfo.ScriptName
                        Line      = $_.InvocationInfo.ScriptLineNumber
                        Column    = $_.InvocationInfo.OffsetInLine
                    }
                }
                $p2.Process($info)
            }
            else
            {
                $p1.Process($_)
            }
        }

        end
        {
            $p1.End()
            $p2.End()
        }
    }
}

function Disable-GridOutput
{
    Remove-Item -Path function:Out-Default -ErrorAction SilentlyContinue
}

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!

Anonymous
Parents
No Data
Comment
  • This is clever and an interesting way to override the default cmdlets of PowerShell, but please don't do this in a function or script that you will push down to others. Changing the default behavior of PowerShell is definitely not a best practice. It is much better to let them decide how to output their content. Also, this is not cross-platform compatible with PowerShell Core since Out-GridView is not available in Core (yet).

Children
No Data