I am learning PowerShell (newbie alert!!) and am trying to figure out why the following strange behavior is seen. (Environment : Windows 10 with PowerShell 5)
C:\>POWERSHELL
Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. All rights reserved.
PS > $A=(1,2,3) # When a variable stores a new array, ...
PS > $A # the elements are shown correctly.
1
2
3
PS > $B=$A # When the array is copied, ...
PS > $B # the elements are shown correctly.
1
2
3
PS > $B=($A,4,5,6) # When the variable stores a new array with elements from some other array ...
PS > $B # the elements are shown correctly.
1
2
3
4
5
6
PS > $B=($B,7,8,9) # But, when the variable stores a new array with elements from the array currently stored in the same variable, ...
PS > $B # something strange is seen
Length : 3
LongLength : 3
Rank : 1
SyncRoot : {1, 2, 3}
IsReadOnly : False
IsFixedSize : True
IsSynchronized : False
Count : 3
4
5
6
7
8
9
PS >
Any pointers on what is going on ?
While typing this question, I was trying to analyze the situation. The way I see it:
$B=($A,4,5,6) makes $B an array with an array element.
$B=($B,7,8,9) makes $B an array with an array element with an array element.
The PowerShell CLI function which shows the variable contents, does not go all the way down to the leaf elements, but stops at the second level.
Hence the inner most array (contents==$A) is shown as some object.
Is this explanation correct ?
$B=($B.Clone(),7,8,9)still shows the same output for $B ]$Bactually has length4, with index0being the entire$Aarray. So yes, your explanation correct