I am Trying To add a System Variable here using PowerShell:
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?
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.
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.
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.
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.
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