views:

133

answers:

2

Typically, in Powershell you would use

env:VARIABLE = "Some kind of value"

But my issue is that I have the name of the variable in a string object. Powershell does not recognize it as a string object and uses the variable name as the name of the environment variable. For example, if I do this:

$someVariable = "MY_ENV_VAR"
env:$someVariable = "Some kind of value"

The result is $someVariable being literally defined as an environment variable instead of MY_ENV_VAR. I've tried numerous iterations of using ${} as if there were periods in the string, but nothing I've found works. How can I use Powershell's Env: using a string object?

+3  A: 

The "Env" drive is a provider so you can use the *-Item cmdlets on it e.g.:

New-Item env:\$someVariable -Value "some kind of value"

Keith Hill
Thank you very much!
nathan
A: 

Set-Variable -Name $someVariable -Value "Some kind of value"

Shay Levy
That works on PowerShell variables but I don't think it works for environment variables.
Keith Hill
Indeed, it doesn't.
Joey
Yup, misread the question :(
Shay Levy