PowerShell v5: Suppress Out-Default output in nested functions

3

3

I'd like to suppress any output then function_b is invoked:

Function function_a {
    "test" | Out-Default
}

function function_b {
    [CmdletBInding()]
    param()

    function_a
}

# These are the things I've tried so far to no avail:
[void](function_b)
$null = function_b
function_b | out-null
function_b *>&1 | Out-null

Windows 10 has introduced a change to the Windows Update logging. Everything's now logged to ETL channels instead of "$env:SystemDrive\Windows\WindowsUpdate.log". In order to get the logs in a human readable format you have to generate the WindowsUpdateLog with the Get-WindowsUpdateLog Command which spews out a lot of useless stuff to the console which I'd like to suppress. As it turns out the command is part of a module and the helper functions in Get-WindowsUpdateLog all use Out-Default. My example illustrates the way that module is built.

megamorf

Posted 2016-03-28T08:45:31.920

Reputation: 1 494

1&{Set-Alias Out-Default Out-Null; Get-WindowsUpdateLog ...} – user364455 – 2016-03-29T08:38:31.230

Answers

2

Thanks @PetSerAl, that should do the trick even though it's not as clean as I had hoped it to be :-)

&{Set-Alias Out-Default Out-Null; Get-WindowsUpdateLog ...}

megamorf

Posted 2016-03-28T08:45:31.920

Reputation: 1 494

0

I found out that its possible, except its still a little bit broken. Example:

   PS> function out-default {$input | out-null}

   #ok. Works great
   PS> mkdir xyz
   # output directory object is sent to null and not displayed to out-host

The problem is that it works TOO well, because let's say you want to save the object to a return varable.

   PS> $dir = mkdir xyz2

   PS> $dir
   #nothing to returned!

On the positive side, the override of out-default is easy to delete:

   PS> del function:out-default

Now the expected default behavior is reverted:

   PS> $dir = mkdir xyz3

   PS> $dir
   Directory: C:\Users\john\sandbox\tmp2
    Mode      LastWriteTime    Length   Name
    ---       -------------    ------   ----
    ---       12/19/2017       1:26 PM  xyz3

   PS> mkdir xyz4
   Directory: C:\Users\john\sandbox\tmp2
   Mode      LastWriteTime    Length   Name
   ---       -------------    ------   ----
   ---       12/19/2017       1:26 PM  xyz4

Would be nice if assigning a variable was separate from out-default. Because then you could just leave the out-default assigned to out-null for the entire script without worrying about breaking the script.

Bill Moore

Posted 2016-03-28T08:45:31.920

Reputation: 191

IMHO, you should change #nothing to returned! to #nothing to displayed! because $dir have value returned from mkdir xyz2, but that value not displayed. Also, if you want to define function, which do nothing, then you do not need $input | out-null part: function out-default { }. – user364455 – 2018-01-05T18:58:42.590