7

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.

1 Answer 1

11

env: a real PSDrive that supports item retrieval:

$val = (Get-Item -Path env:\$varName).Value
Sign up to request clarification or add additional context in comments.

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.