#powerShell Inhibit Output Echo from ArrayList Method

For optimal performance, creation of an empty array using
$a = New-Object System.Collections.ArrayList

is, for instance, preferred over
$a = @()

However, putting a single element into the array with the former’s Add() method will auto output the index of the just inserted item. If left unchecked, this can become part of the output in a PowerShell function call which may not be desirable.

To resolve this, simply pipe the call to Out-Null like
$a.Add((Get-Date)) | Out-Null

or prefix with [void] to suppress the echo:
[void]$a.Add((Get-Date))

Leave a Reply