Home Use variable in other variable name
Reply: 2

Use variable in other variable name

Danijel de Vasco
1#
Danijel de Vasco Published in 2018-01-10 10:46:04Z

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
gvee
2#
gvee Reply to 2018-01-10 10:47:58Z

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
Esperento57
3#
Esperento57 Reply to 2018-01-10 18:16:58Z

you can do it too:

Invoke-Expression $"GV_$m`_var"
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31729 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO