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

2 Answers 2

1

Use the Get-Variable cmdlet:

Write-Host $(Get-Variable "s$i" -ValueOnly)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Exactly what I needed. Worked like a charm.
1

You are looking for the Get-Variable cmdlet:

Get-Variable 'i'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.