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,570
  • 2
  • 13
  • 29

2 Answers2

2

Try this:

(Get-Variable -Name $varName).Value

Or this: (as suggested by mklement0)

Get-Variable -Name $varName -ValueOnly
String.Empty
  • 137
  • 11
0

A bit more awkward answer.

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

Or

(dir variable:$a).value
js2010
  • 17,785
  • 4
  • 45
  • 50