$data = @()
This works....
$MyArray = @(
"Item0";
"Item1";
"Item2";
)
Hell, even this works:
$MyArray = "Item0", "Item1", "Item2";
Both have same type:
> $MyArray.GetType()
result:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
And both contain strings...
> $MyArray | % { $_.GetType() }
result:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
True True String System.Object
True True String System.Object
So I think the second one is simpler. Go with that.
Access items by index offset (this offset like most offsets is zero-based)
$MyArray[0]
$MyArray[0,5,12]
and
$MyArray[1..3]
and
$MyArray[-1..-4]
$MyArray.count
$MyArray[-1]
$MyArray | ForEach-Object {"Item: [$PSItem]"}
returns
Item: [Zero]
Item: [One]
Item: [Two]
Item: [Three]
To add an item to an array in powershell - we do not push or concatenate - we "plus equals" or +=
$data = @(
'Zero'
'One'
'Two'
'Three'
)
$data += 'four'