Discarding (Any) Output

by Jul 9, 2020

There are (a few) commands in PowerShell that output information to the console no matter what you do. Neither redirection of streams nor assigning to $null will silence such commands, for example:

 
PS> $null = Get-WindowsUpdateLog *>&1  
 

Even though all output streams are discarded, the Get-WindowsUpdateLog cmdlet still writes extensive information to the console.

If you come across such a situation, a last resort is to temporarily disable the internal command Out-Default like this:

# temporarily overwrite Out-Default
function Out-Default {}

# run your code (guaranteed no output)
Get-WindowsUpdateLog

# test any other direct console write
[Console]::WriteLine("Hello")

# restore Out-Default
Remove-Item -Path function:Out-Default


Twitter This Tip! ReTweet this Tip!