Skip to main content

How to Hide a Command's Output in PowerShell with Out-Null

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Some PowerShell commands print something you didn't ask for. Adding an item to an ArrayList, for example, prints the item's position. Piping the command to Out-Null throws that output away. This guide shows the difference, with real output from PowerShell 7.6.10.

Syntax​

Syntax
<command> | Out-Null
PartWhat it means
<command>Runs as usual, side effects included
| Out-NullReceives its output and discards it

The reference: "The Out-Null cmdlet sends its output to NULL, in effect, removing it from the pipeline and preventing the output from being displayed on screen."

Prerequisites​

  • PowerShell 7. This guide uses PowerShell 7.6.10.

Step 1: Add Two Names, One Quietly​

PowerShell
$list = [System.Collections.ArrayList]::new()
$list.Add("Ada")
$list.Add("Grace") | Out-Null
"The list has $($list.Count) names."
Output
0
The list has 2 names.

PowerShell adding two names to an ArrayList: the first Add printed 0, the second, piped to Out-Null, printed nothing, and the list has 2 names.

The first Add printed 0, the position of Ada. The second added Grace just the same, but Out-Null swallowed its 1.

Common Mistakes​

MistakeWhat happensDo this instead
Piping to Out-Null to hide errorsErrors don't go through the pipeline: Get-Content missing.txt | Out-Null still printed Cannot find path ...Handle them with try and catch, or redirect the error stream
Using it where you need the output laterThe output is goneStore it in a variable instead

Troubleshooting​

SymptomLikely causeFix
Stray numbers appear in a script's outputA method such as ArrayList.Add returns a value, and PowerShell prints itAdd | Out-Null to that line

FAQ​

Are there other ways to discard output?​

Yes: > $null, [void] before the expression and $null = ... all discard it too. With one name added each way, the list ended with 3 names and nothing else was printed.

Does Out-Null stop the command from running?​

No. The command runs fully; only its output is thrown away, as the list's count of 2 shows.

Conclusion​

$list.Add("Grace") | Out-Null added the name without printing its index, while the first Add printed 0. Use it on any line whose output you don't need.

Additional Resources​