Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

You can't expand array[index] in a string

Context:

You can expand variables in a string like:

> $var = "123"
> Write-Host "$var"
123
>

Problem:

You can’t expand array with index:

> $arr = @("456", "789")
> Write-Host "$arr[0]"
456 789[0]

It is also ineffective to add braces:

> Write-Host "${arr}[0]"
456 789[0]
> Write-Host "${arr[0]}"

>

Reason:

It seems by design.

Solution:

You should to use placeholders and -F option like:

> Write-Host ("{0}" -F $arr[0])
456