2

I need values of variables which these variable names are concatened by another variable's value.

e.g.

$GV_R32_var = "testtesttest"
$m = "R32"
(Get-Variable -Name "`$GV_$($m)_var").Value

I receive following error message:

+ (Get-Variable -Name "`$GV_$($m)_var").Value
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ($GV_R32_var:String) [Get-Variable], ItemNotFoundException
    + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand

2 Answers 2

4

You're really close!

The variable name doesn't include a dollar!

So your code:

(Get-Variable -Name "`$GV_$($m)_var").Value

Becomes:

(Get-Variable -Name "GV_$($m)_var").Value
Sign up to request clarification or add additional context in comments.

Comments

0

you can do it too:

Invoke-Expression $"GV_$m`_var"

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.