4

I am trying to figure out an easy to read and understandable idiom that evaluates the the value of an environment variable whose name is stored in a variable:

$varName='TEMP'

I have come up with

$val = invoke-expression "`$env:$varName"

and am wondering if there is a more to the point alternative.

mklement0
  • 382,024
  • 64
  • 607
  • 775
René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293

1 Answers1

7

env: a real PSDrive that supports item retrieval:

$val = (Get-Item -Path env:\$varName).Value
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206