Powershell Simple Array output incorrect?

Viewed 38
$nameonly =  @("") * 20 

$i=1 
do 
{
    $nameonly[$i] = "A$i" 
    $i++
}
until ($i -eq 10)

write-host "$nameonly[4]"

When i run the code i expect the output to be: A4 but instead i get:

A1 A2 A3 A4 A5 A6 A7 A8 A9 [4]

`

1 Answers

Try removing the quotation marks in your final command. So it should be write-host $nameonly[4] This will get you the indexed value in position 4 of the array.

You can also skip the write-host command. Invoking the variable $nameonly[4] will output to the console.

If you use quotation marks then it will convert the whole array into a string and output the entire array content as a string object.

Related