# PowerShell: \$null VS Out-Null

Be sure to check out my Scripting4Crypto initiative. It’s a fun way to get into using cryptocurrencies all while getting your PowerShell needs met.

I have been doing a bit of research and a lot of testing lately regarding the use of the Out-Null cmdlet. Typically, I have been using the Out-Null when adding members to ArrayLists, columns and rows to DataTables, etc. , to suppress the output. I don’t want to clutter the console with superfluous data. So, to accomplish this, I simply pipe the the results of a cmdlet to the Out-Null as shown below. Works great. Or so I thought. It may not be the best option.

In the following code snippet, I create a new arraylist and populate it with the numbers 1 thru 100, while piping the output to the Out-Null cmdlet.

```# Creates a new ArrayList to be populated with the numbers 1 - 100.
\$arrayList = New-Object System.Collections.ArrayList

# A simple example of populating an ArrayList
1 .. 100 | ForEach-Object { \$arrayList.Add(\$_) | Out-Null }```

Next, I will wrap the existing script block with the Measure-Command to determine how long it takes to run on my computer.

```Measure-Command {
# Creates a new ArrayList to be populated with the numbers 1 - 100.
\$arrayList = New-Object System.Collections.ArrayList

`# A simple example of populating an ArrayList `
`1 .. 100 | ForEach-Object { \$arrayList.Add(\$_) | Out-Null }`
}``` Results of the Measure-Command with Out-Null

It took 9.6927 milliseconds to run. Fast, right?

What I have found it that it is significantly slower than populating a variable with the output. So, now lets do the same, except output to a variable (\$null).

```Measure-Command {
\$arrayList_Variable = New-Object System.Collections.ArrayList

`1 .. 100 | ForEach-Object { \$null = \$arrayList.Add(\$_) }` }``` Results of the Measure-Command with Variable

It only took 2.7689 milliseconds to run when outputting to a variable. That is considerably faster.

OK, so this could just be a coincidence, right? Well, let’s run each multiple times and get an average. The following code snippet will run the Measure-Command for both scenarios 10 times and generate an average.

```# Get average times for each scenario.
[int] \$NumberOfTimesToRun = 10
[double] \$total_OutNull = 0
[double] \$total_Variable = 0

for (\$x = 0; \$x -lt \$NumberOfTimesToRun; \$x++) {
\$results_OutNull = Measure-Command {
# Creates a new ArrayList to be populated with the numbers 1 - 100.
\$arrayList = New-Object System.Collections.ArrayList
`# A simple example of populating an ArrayList`
```1 .. 100 | ForEach-Object { \$arrayList.Add(\$_) | Out-Null }
} ```
# Add the results to the corresponding variable.
`\$total_OutNull += \$results_OutNull.TotalMilliseconds `

`    \$results_Variable = Measure-Command { `
```# Creates a new ArrayList to be populated with the numbers 1 - 100.
```        `\$arrayList = New-Object System.Collections.ArrayList `
```# A simple example of populating an ArrayList
``````         1 .. 100 | ForEach-Object { \$null = \$arrayList.Add(\$_) }
} ```
# Add the results to the corresponding variable.
`    \$total_Variable += \$results_Variable.TotalMilliseconds`
}

# Get the average run-times.
\$average_OutNull = \$total_OutNull / \$NumberOfTimesToRun
\$average_Variable = \$total_Variable / \$NumberOfTimesToRun```

In the following image you will see that the averages support my observations. Using the Out-Null is much slower than outputting to a variable.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8117721,"math_prob":0.7515307,"size":3655,"snap":"2022-40-2023-06","text_gpt3_token_len":872,"char_repetition_ratio":0.13694878,"word_repetition_ratio":0.2568027,"special_character_ratio":0.24842681,"punctuation_ratio":0.13193403,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96316934,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-07T02:31:42Z\",\"WARC-Record-ID\":\"<urn:uuid:7bf92702-7376-4af9-9e48-7070c83cf484>\",\"Content-Length\":\"93544\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4583c573-0773-47db-95e7-5b2416d261b7>\",\"WARC-Concurrent-To\":\"<urn:uuid:b2b080a1-e6f8-4182-a378-88a202538437>\",\"WARC-IP-Address\":\"192.0.78.25\",\"WARC-Target-URI\":\"https://powerwisescripting.blog/2020/04/15/powershell-null-vs-out-null/\",\"WARC-Payload-Digest\":\"sha1:QMPK7Q2W4Q3MN43VCDGQLX7AS3JZGJEX\",\"WARC-Block-Digest\":\"sha1:J2VWP24GFXQ3TBVTAOP27TC5MPZDP2OT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337906.7_warc_CC-MAIN-20221007014029-20221007044029-00051.warc.gz\"}"}