What's the better (cleaner) way to ignore output in PowerShell? [closed] What's the better (cleaner) way to ignore output in PowerShell? [closed] powershell powershell

What's the better (cleaner) way to ignore output in PowerShell? [closed]


I just did some tests of the four options that I know about.

Measure-Command {$(1..1000) | Out-Null}TotalMilliseconds : 76.211Measure-Command {[Void]$(1..1000)}TotalMilliseconds : 0.217Measure-Command {$(1..1000) > $null}TotalMilliseconds : 0.2478Measure-Command {$null = $(1..1000)}TotalMilliseconds : 0.2122## Control, times vary from 0.21 to 0.24Measure-Command {$(1..1000)}TotalMilliseconds : 0.2141

So I would suggest that you use anything but Out-Null due to overhead. The next important thing, to me, would be readability. I kind of like redirecting to $null and setting equal to $null myself. I use to prefer casting to [Void], but that may not be as understandable when glancing at code or for new users.

I guess I slightly prefer redirecting output to $null.

Do-Something > $null

Edit

After stej's comment again, I decided to do some more tests with pipelines to better isolate the overhead of trashing the output.

Here are some tests with a simple 1000 object pipeline.

## Control PipelineMeasure-Command {$(1..1000) | ?{$_ -is [int]}}TotalMilliseconds : 119.3823## Out-NullMeasure-Command {$(1..1000) | ?{$_ -is [int]} | Out-Null}TotalMilliseconds : 190.2193## Redirect to $nullMeasure-Command {$(1..1000) | ?{$_ -is [int]} > $null}TotalMilliseconds : 119.7923

In this case, Out-Null has about a 60% overhead and > $null has about a 0.3% overhead.

Addendum 2017-10-16: I originally overlooked another option with Out-Null, the use of the -inputObject parameter. Using this the overhead seems to disappear, however the syntax is different:

Out-Null -inputObject ($(1..1000) | ?{$_ -is [int]})

And now for some tests with a simple 100 object pipeline.

## Control PipelineMeasure-Command {$(1..100) | ?{$_ -is [int]}}TotalMilliseconds : 12.3566## Out-NullMeasure-Command {$(1..100) | ?{$_ -is [int]} | Out-Null}TotalMilliseconds : 19.7357## Redirect to $nullMeasure-Command {$(1..1000) | ?{$_ -is [int]} > $null}TotalMilliseconds : 12.8527

Here again Out-Null has about a 60% overhead. While > $null has an overhead of about 4%. The numbers here varied a bit from test to test (I ran each about 5 times and picked the middle ground). But I think it shows a clear reason to not use Out-Null.


I realize this is an old thread, but for those taking @JasonMArcher's accepted answer above as fact, I'm surprised it has not been corrected many of us have known for years it is actually the PIPELINE adding the delay and NOTHING to do with whether it is Out-Null or not. In fact, if you run the tests below you will quickly see that the same "faster" casting to [void] and $void= that for years we all used thinking it was faster, are actually JUST AS SLOW and in fact VERY SLOW when you add ANY pipelining whatsoever. In other words, as soon as you pipe to anything, the whole rule of not using out-null goes into the trash.

Proof, the last 3 tests in the list below. The horrible Out-null was 32339.3792 milliseconds, but wait - how much faster was casting to [void]? 34121.9251 ms?!? WTF? These are REAL #s on my system, casting to VOID was actually SLOWER. How about =$null? 34217.685ms.....still friggin SLOWER! So, as the last three simple tests show, the Out-Null is actually FASTER in many cases when the pipeline is already in use.

So, why is this? Simple. It is and always was 100% a hallucination that piping to Out-Null was slower. It is however that PIPING TO ANYTHING is slower, and didn't we kind of already know that through basic logic? We just may not have know HOW MUCH slower, but these tests sure tell a story about the cost of using the pipeline if you can avoid it. And, we were not really 100% wrong because there is a very SMALL number of true scenarios where out-null is evil. When? When adding Out-Null is adding the ONLY pipeline activity. In other words....the reason a simple command like $(1..1000) | Out-Null as shown above showed true.

If you simply add an additional pipe to Out-String to every test above, the #s change radically (or just paste the ones below) and as you can see for yourself, the Out-Null actually becomes FASTER in many cases:

$GetProcess = Get-Process# Batch 1 - Test 1 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { $GetProcess | Out-Null } }).TotalMilliseconds# Batch 1 - Test 2 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { [void]($GetProcess) } }).TotalMilliseconds# Batch 1 - Test 3 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { $null = $GetProcess } }).TotalMilliseconds# Batch 2 - Test 1 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { $GetProcess | Select-Object -Property ProcessName | Out-Null } }).TotalMilliseconds# Batch 2 - Test 2 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { [void]($GetProcess | Select-Object -Property ProcessName ) } }).TotalMilliseconds# Batch 2 - Test 3 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { $null = $GetProcess | Select-Object -Property ProcessName } }).TotalMilliseconds# Batch 3 - Test 1 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { $GetProcess | Select-Object -Property Handles, NPM, PM, WS, VM, CPU, Id, SI, Name | Out-Null } }).TotalMilliseconds# Batch 3 - Test 2 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { [void]($GetProcess | Select-Object -Property Handles, NPM, PM, WS, VM, CPU, Id, SI, Name ) } }).TotalMilliseconds# Batch 3 - Test 3 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { $null = $GetProcess | Select-Object -Property Handles, NPM, PM, WS, VM, CPU, Id, SI, Name } }).TotalMilliseconds# Batch 4 - Test 1 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { $GetProcess | Out-String | Out-Null } }).TotalMilliseconds# Batch 4 - Test 2 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { [void]($GetProcess | Out-String ) } }).TotalMilliseconds# Batch 4 - Test 3 (Measure-Command { for ($i = 1; $i -lt 99; $i++) { $null = $GetProcess | Out-String } }).TotalMilliseconds


There is also the Out-Null cmdlet, which you can use in a pipeline, for example, Add-Item | Out-Null.

Manual page for Out-Null

NAME    Out-NullSYNOPSIS    Deletes output instead of sending it to the console.SYNTAX    Out-Null [-inputObject <psobject>] [<CommonParameters>]DETAILED DESCRIPTION    The Out-Null cmdlet sends output to NULL, in effect, deleting it.RELATED LINKS    Out-Printer    Out-Host    Out-File    Out-String    Out-DefaultREMARKS     For more information, type: "get-help Out-Null -detailed".     For technical information, type: "get-help Out-Null -full".