3

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?

2
  • Are you wanting to create a variable name based on the value of another varible? So, $abc = "xyz" and you want to create a variable name of $xyz? Commented Sep 7, 2017 at 18:51
  • I want to call a variable of $xyz, it already exists. Commented Sep 7, 2017 at 18:52

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.