Brian
Brian

Reputation: 13593

Echoing an environment variable based on another variable

Let's say I have an environment variable called test1 whose value is test_value. How can I display test_value by using another variable $target='test1'?

Here are what I've tried:

$target='test1'
echo $env:$target    
echo $env:$($target) 
echo "$env:$target"

All of the 3 methods above fail to display test_value. How can I achieve my goal?

Upvotes: 0

Views: 39

Answers (1)

David Brabant
David Brabant

Reputation: 43569

$env:test1 = 'test_value'
$target = 'test1'
$value = Invoke-Expression ('$env:' + $target)
$value

Upvotes: 1

Related Questions