powershellpuppet

How to dynamically create an environment variable?


I am using powershell script to set some environment variable--

$env:FACTER_Variable_Name = $Variable_Value

FACTER is for using these in the puppet scripts.

My problem is - the variable name and variable value both are dynamic and getting read from a text file.

I am trying to use

$env:FACTER_$Variable_Name = $Variable_Value

But $ is not acceptable syntax. When I enclose it in double quotes, the variable value is not getting passed. Any suggestion how to use it dynamically.

Thanks in Advance


Solution

  • [Environment]::SetEnvironmentVariable("TestVariable", "Test value.", "User")

    This syntax allows expressions in the place of "TestVariable", and should be enough to create a profile-local environment variable. The third parameter can be "Process", this makes new vars visible in Get-ChildItem env: or "Machine" - this required administrative rights to set the variable. To retrieve a variable set like this, use [Environment]::GetEnvironmentVariable("TestVariable", "User") (or matching scope if you choose another).