I am very much puzzled with the way PowerShell is behaving with the arrays . In the below code I am adding three entries in the an array and when I check in the function it does give me the array.Count as 3, which is very much on the expected lines. Now I call the function at a different place in the code and the element count just doubles from 3 to 6. I am sure doing something wrong here. Anyone has any idea/thoughts?
function ReadAPEnvInfoFrom () {
$pathList = New-Object System.Collections.ArrayList
$pathList.Add("aaa")
$pathList.Add("bbb")
$pathList.Add("ccc")
Write-Host 'The count' $pathList.Count
# returns 3
return $pathList
}
cls
$Array = New-Object System.Collections.ArrayList
$Array = ReadAPEnvInfoFrom
Write-Host 'The count' $Array.Count
# returns 6
$pathListdefined?$Arrayshould probably be$pathListin your sample code. If I run it the count is increased by 3 each time the function is called, just as expected.$pathList.Add("xxx")->[void]$pathList.Add("xxx"), also no need for$Array = New-Object System.Collections.ArrayListas you replace$Arrayvalue on next line. Andreturn $pathList->return ,$pathListif you actually want to returnArrayListfrom function but not its elements.