Gotcha Ignoring unwanted output

suggest change

Inspired by

function bar {
 [System.Collections.ArrayList]$MyVariable = @()
 $MyVariable.Add("a") | Out-Null
 $MyVariable.Add("b") | Out-Null
 $MyVariable
}

The Out-Null is necessary because the .NET ArrayList.Add method returns the number of items in the collection after adding. If omitted, the pipeline would have contained 1, 2, "a", "b"

There are multiple ways to omit unwanted output:

function bar
{
    # New-Item cmdlet returns information about newly created file/folder
    New-Item "test1.txt" | out-null
    New-Item "test2.txt" > $null
    [void](New-Item "test3.txt")
    $tmp = New-Item "test4.txt"
}

Note: to learn more about why to prefer > $null, see [topic not yet created].

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Return behavior in PowerShell:
* Gotcha Ignoring unwanted output

Table Of Contents
1 Loops
18 Return behavior in PowerShell
19 XML
22 Strings
25 Aliases
47 MongoDB
65 AWS S3