is there a way to supress the return value (=Index) of an ArrayList in Powershell (using System.Collections.ArrayList) or should I use another class?
$myArrayList = New-Object System.Collections.ArrayList($null)
$myArrayList.Add("test")
Output: 0
is there a way to supress the return value (=Index) of an ArrayList in Powershell (using System.Collections.ArrayList) or should I use another class?
$myArrayList = New-Object System.Collections.ArrayList($null)
$myArrayList.Add("test")
Output: 0
You can cast to void to ignore the return value from the Add method:
[void]$myArrayList.Add("test")
Another option is to redirect to $null:
$myArrayList.Add("test") > $null
Write-Output) when part of an existing sequence. E.g. I have an arraylist $ADUsersList that I add a record to via .Add(). When I then try to Write-Output $ADUsersList as part of the script, it's blank in my IDE. But as soon as the script is done running, and I just check $ADUsersList for contents, they get printed as I would expect.$l = [System.Collections.ArrayList]::new(); $l.Add("test") > $null; $l[void]$ArrayList.Add($objRecord). It worked if I checked $ArrayList after the script ran, but during the script any Write-Output attempts at displaying $ArrayList's contents failed). Anyway, glad to see I was mistaken. It means debugging w/ ArrayLists is slightly easier than I thought.Two more options :)
Pipe to out-null
$myArrayList.Add("test") | Out-Null
Assign the result to $null:
$null = $myArrayList.Add("test")