Say I have the following environment variables:
a = Poke
b = mon
Pokemon= Feraligatr
I want to be able to concatenate a and b environment variables to get the variable name Pokemon and the get Pokemon value like $($env:ab) or $($env:$($env:a)$($env:b)) (This examples does not work)
Building on the helpful comments:
You're looking for indirection, i.e. the ability to refer to an environment variable indirectly, via another (environment) variable(s) storing the target variable's name.
PowerShell-idiomatic solution:
Use the
Env:drive in combination with theGet-Contentcmdlet:Note:
To set environment variables indirectly, use the
Set-Contentcmdlet; e.g.:Applying the same technique to regular shell variables (non-environment variables), requires either use of the
variable:drive, or, for more flexibility, theGet-VariableandSet-Variablecmdlets - see this answer.More more information about expandable (interpolating) string literals such as
"env:$env:a$env:b", see the conceptual about_Quoting help topic..NET API alternative:
As Max points out, you can also use the static
System.Environment.GetEnvironmentVariable.NET method:For more information about calling .NET API methods from PowerShell, see the conceptual about_Methods help topic.