Gotcha Ignoring unwanted output
suggest changeInspired by
function bar {
[System.Collections.ArrayList]$MyVariable = @()
$MyVariable.Add("a") | Out-Null
$MyVariable.Add("b") | Out-Null
$MyVariable
}
Out-Null is necessary because the .NET ArrayList.Add method returns the zero-based index of the new item. If you omit it, the output is 0, 1, "a", "b", not just the two strings.
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].
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents