0

I am newbie in powershell and have some problem.

Scenario

In my pipeline and scripts I have defined variable $myVar = 'someValue'. I store its name in $varName = 'myVar'.

Question

Is it possible to get myVar value by referencing $varName? I Tried something like: $($varName) but it returned only myVar, not 'someValue'

zolty13
  • 1,943
  • 3
  • 17
  • 34

2 Answers2

2

Try this:

(Get-Variable -Name $varName).Value

Or this: (as suggested by mklement0)

Get-Variable -Name $varName -ValueOnly
String.Empty
  • 145
  • 13
0

A bit more awkward answer.

$a = 'b'
$b = 1
(dir variable:).where{$_.name -eq $a}.value
1

Or

(dir variable:$a).value
js2010
  • 23,033
  • 6
  • 64
  • 66