2

I have 3 environments to work with, and need to choose a value based on which environment I'm in. I have a dynamic variable $environment that could be either development, test, or production. I want to also have other variables:

$development
$test
$production

that have values to be used based on which environment I'm working in.

Is there some way of calling $($environment) and using the result of the environment variable as another variable? To clarify:

$abc = "prod" $prod = "production-value"

I want to call something like $($abc) and have that return "production-value"

A similar question was asked here but for Ruby; is there a way to do this in Powershell?

user3364161
  • 365
  • 5
  • 21

1 Answers1

4

You can do this with Get-Variable:

$environment = 'test'

$test = 'test'
$prod = 'prod'

(Get-Variable -Name $environment).value

Would return the value of the $test variable.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • 6
    Beware that whenever you feel the need to actually do this there's a greater than average chance that you should consider using a different data structure instead (e.g. an array or a hashtable). – Ansgar Wiechers Sep 07 '17 at 19:00