Discarding Unwanted Information

by Jul 20, 2012

If you want to dump results from a command, there are a number of ways. While they all do the same, they have tremendous performance differences:

PS> (Measure-Command {1..100000 | Out-Null }).TotalMilliseconds
1114,3006 #!!!!!!!!!!! 
PS> (Measure-Command {$null = 1..100000 }).TotalMilliseconds
PS> (Measure-Command {1..100000 > $null }).TotalMilliseconds
PS> (Measure-Command {[void](1..100000) }).TotalMilliseconds

As you can see, piping results to Out-Null is the worst approach. Instead, simply assign unwanted results to the special variable $null or cast the call to [void].

Twitter This Tip! ReTweet this Tip!