1

I have some predefined variables, eg:

$s1 = a
$s2 = b
...
$s[x] = x

I have a script that connects to a device via serial port, get some data, split the string and get a value I need. The value that I have from the string I needed to use it to get a predefined variable.

In php I do it easily like this:

$var1 = a
$var2 = b
...
$var[x] = x


$i = something
echo ${"var_name$i"}

How can I do the same in PowerShell? I tried

write-host $s$i
write-host $s{$i}
write-host ${s$i}
write-host ${"s$i"}

with no luck...all I get is either the value of $i or empty space

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
xlucian
  • 339
  • 2
  • 4
  • 19

2 Answers2

1

You are looking for the Get-Variable cmdlet:

Get-Variable 'i'
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
1

Use the Get-Variable cmdlet:

Write-Host $(Get-Variable "s$i" -ValueOnly)
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206