Consider the following small script:
$a = @(1,2,3) #1 $b = $a #2 $a = 101 #3 $b += 99 #4 Write-Host "a: $a" Write-Host "b: $b"
The output is this which required me to think a bit:
a: 101 2 3 b: 101 2 3 99
My observations for clarification:
- The $b array contains four elements in the output so $b = $a is creating a copy of original array with three items in it and then the 99 element is added. If $b = $a was a pointer operation, then adding an element to $b would effect the $a array as well
- The array contains pointers to the integers and not the integers themselves. Proved by the fact that line #3 changes the contents of both arrays. If $a contained integers, then $b wouldn't have been affected
So my understanding is:
A PowerShell array is always an array of pointers even if it contains just integers. Assigning an array to another variable creates a copy of that array of pointers.