in reply to How to access system environment variables?

I'd just like to emphasize that under WinNT and later, "a system environment variable" means much more than just "an environment variable". WinNT/Win2K environment variables can be "system", "user", or "temporary". "system" ones are set for all processes. "user" ones are set when that particular user logs in. "temporary" ones are set with "SET X=Y" or $ENV{X}="Y".

Getting and setting "system" and "user" environment variables is quite easy using Win32::TieRegistry. There are a couple of things to worry about.

First, don't read a variable via $ENV{PATH} if you want to modify it permanently. One of our best programmers did that and it was a huge mess (more on this later). Second, be sure to enable ArrayValues so you don't change the type of the registry value.

For example, say the "system" "PATH" variable is just the standard %SystemRoot%\System32;%SystemRoot% and your "user" "PATH" is C:\Perl\bin. You log in one day, happen to do SET PATH=%PATH%;C:\doom before you run your neat little script. Then $ENV{PATH} will return "C:\\WinNT\\System32;C:\\WinNT;C:\\Perl\\bin;C:\\doom". Then you update the "system" "PATH" and the next time you log in your PATH is C:\WinNT\System32;C:\WinNT;C:\Perl\bin;C:\doom;C:\Perl\bin. Note the duplicate and how your temporary change has now become permanent.

Update: I have posted a nice little script I have that demonstrates how to do this correctly and also can fix the above mistakes automatically for you.

Note that changes to "system" environment variables via the registry do not take full effect until you reboot. You can make them take almost full effect by properly diddling (that is the technical term for it) with the "Environment" tab of the "System" control panel applet and logging out and back in again.

        - tye (but my friends call me "Tye")