views:

93

answers:

3

I have a script that sets an environment variable in Windows XP by creating a value in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment. The variable shows up when I view the env var GUI under Control Panel, but if I type SET in the command window it isn't listed. If I try to echo it in the command window, it doesn't get any value. How do I set the environment variable correctly from a script, preferably using the registry?

+1  A: 

From within a script you can only set a variable using the SET command:

SET my_var=42

But that will not change/define this variable "globally". As soon as you close the commandline window where that script was run, the variable is gone.

So if you want to define the variable permanently, you'll have to combine your registry solution with a SET command.

a_horse_with_no_name
so in addition to what I'm doing in the registry, I'll also have to open the command line and use SET to set my env var there as well to make it stick?
anon
No, just use the SET command inside your script (I assume we _are_ talking about a batch script?)
a_horse_with_no_name
actually it's an InstallScript file. I need to permanently set the environment variable.
anon
OK, then disregard my answer. I was assuming a batch script
a_horse_with_no_name
A: 

Actually, you can use the DOS command "setx" to set the variable permanently (without having to manually - or programmatically - hack the registry).

I've never used "InstallScript" per se, but most installer productions (like InstallShield, or InnoSetup) have "set system variable" and "set user variable" commands you can use in your script, too.

Here's the MSDN reference on the Windows "setx" command: http://technet.microsoft.com/en-us/library/cc755104%28WS.10%29.aspx