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
29,3192
PS> (Measure-Command {1..100000 > $null }).TotalMilliseconds
38,1542
PS> (Measure-Command {[void](1..100000) }).TotalMilliseconds
28,2378

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!