In Powershell, how do I set an environment variable by supplying the name as a $var?

environment-variables, powershell

Solution

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"

Problem

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 have found works. How can I use PowerShell's `Env:` using a string object?

Original source