Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Add System Variable

I am Trying To add a System Variable here using PowerShell:

Environment Var dialog

I have tried both ways using

$env:MyTestVariable = "My test variable." 

and

[Environment]::SetEnvironmentVariable("TestVariableName", "My Value", "<option>") 

However neither of them seem to add to this section. I have tried restarting the computer as well to see if it would take effect then. I have looked at technet along with countless other websites, but nothing I have tried has worked.

How can I set a system variable with PowerShell?

like image 734
Tyler S Avatar asked Sep 23 '15 03:09

Tyler S


People also ask

How do I add a system variable in PowerShell?

To set the environmental variable using PowerShell you need to use the assignment operator (=). If the variable already exists then you can use the += operator to append the value, otherwise, a new environment variable will be created.

How do I add system variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How do I permanently set an environment variable in PowerShell?

Using $env:Path variable in PowerShell to set environment variable for the session or temporary. Use [System. Environment] method in PowerShell to set environment variable permanently.


1 Answers

Run PowerShell as an administrator (to get the necessary registry access permissions) then call out to the .Net framework to set it:

[Environment]::SetEnvironmentVariable("MyTestVariable", "MyTestValue", "Machine") 

NB. it won't take effect within the same process, you'll have to make a new PowerShell process to see it.

like image 118
TessellatingHeckler Avatar answered Sep 28 '22 00:09

TessellatingHeckler