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?
Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit.
In PowerShell, variables are represented by text strings that begin with a dollar sign ( $ ), such as $a , $process , or $my_var . Variable names aren't case-sensitive, and can include spaces and special characters.
The command uses the Set-Variable cmdlet to create the variable. It uses the PassThru parameter to create an object representing the new variable, and it uses the pipeline operator ( | ) to pass the object to the Format-List cmdlet.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With