views:

13

answers:

0

I am running a Windows 7 system where i want to be able to set a system environment variable and be able to see it in subsequent command prompts that i launch.

When i set the system environment variable using "setx" thins work fine, But given that i want to use the same script code for XP and Windows 7 I cannot do this on XP because XP does not come by default with setx.

So i have to resort to using WMI to set the environment variables:

I use this following code to set the variable :

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objVariable = objWMIService.Get("Win32_Environment").SpawnInstance_

objVariable.Name = "TestKey"
objVariable.UserName = "<System>"
objVariable.VariableValue = "TestValue"
objVariable.Put_

But now, when i launch a new command prompt, the environment variable does not take effect; However if i restart the system it takes effect; And when i go to MyComputer>Properties>Advanced System Settings> Environment Variables, then i see the "TestKey" value present there; And when i hit "OK" button there and then launch a new command prompt, the "TestKey" value seems to magically appear;

Is there someway I can make the system environment variable to appear on the subsequent command prompts without resorting to clicking on OK at the environment Variables Window (in System Properties) or restarting the machine?