Search code examples
javascriptenvironment-variableswmiwmi-query

How to modify environment variables using WMI and JavaScript?


I need to access an environment variable and modify its value. I can access the variable using WQL ==>

wmi.ExecQuery("Select * from Win32_Environment Where name='Path' And UserName='<System>'");

However, I am not sure how to modify and save the value. I am using:

var reg = GetObject("winmgmts:/root/cimv2");    
var paths = wmi.ExecQuery("Select * from Win32_Environment Where name='AA' And UserName='<System>'");
var items = new Enumerator(paths);
var path = items.item();
path.VariableValue = path.VariableValue + ";" + "random";
path.Put_(); //(as per first answer received)

But, I get this error:

Access denied 
Code 80041003 
Source SWbemObjectEx

I have UAC disabled, not sure what to do here.

Any help will be appreciated.

Thanks.


Solution

  • After you change the VariableValue, call Put_ to apply the changes:

    path.VariableValue = path.VariableValue + ";" + "random";
    path.Put_();