0

I am trying to create Environment variable for machine through powershell and passing the variables at run-time but It's not taking the variables. Powershell file: env_variable.ps1

Param (
  [Parameter(Mandatory=$True)]  [String] $VAULT_TEXT,
  [Parameter(Mandatory=$True)]  [String] $VAL
 )
[System.Environment]::SetEnvironmentVariable("$VAULT_TEXT","$VAL",[System.EnvironmentVariableTarget]::Machine)

Trying to trigger through Powershell:

$vault_text='IAndAAuth'
$val = get-content 'C:\vault\service_acct_pass'
C:\vault\env_variable.ps1 -VAULT_TEXT "$vault_text" -VAL "$val"

Result is coming like this: Key:VAULT_TEXT, Value:VAL in environments value.

Can you please help me in this.

2
  • It works for me. How do you read the result? Did you start a new session? Does it concern a virtual machine? Commented Apr 2, 2020 at 9:00
  • BTW, there should be no need to quote the variables as they are already defined [string] Commented Apr 2, 2020 at 9:13

1 Answer 1

1

Your method seems right and should be working. You can try doing it the old way by creating a registry key for your environment variable.

User

New-ItemProperty "HKCU:\Environment" -Name "your variable name" -Value "your value"

Machine

New-ItemProperty "HKLM:\System\CurrentControlSet\Control\Session Manager\Environment" -Name "your variable name" -Value "your value"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.