joeware - never stop exploring... :)

Information about joeware mixed with wild and crazy opinions...

Setting persistent environment variables from the command line (or I set an environment variable in a logon script but it doesn’t work!!!)

by @ 6:14 pm on 12/22/2005. Filed under tech

Let’s say you want to set some environment variables for a user as they log on… how do you do it?

Well a logon script of course.

But then…. you figure out, it isn’t working right… The script is running, you are sure of it. But the user can’t use the variables because they are not there, what in the world is wrong?

Well you need to learn the difference between environment variables and PERSISTENT environment variables.

When you set an environment variable in one process with say

Set SomeVar=joeIsCool

it doesn’t magically hop over to other processes that exist or even that you spawn later unless they are spawned from the process you set the environment variable in the first place (say by running start cmd from another cmd).

As soon as you close out the process (or any spawned processes) where you set that environment variable, it goes away. So yes, this means if you set an environment variable in the way mentioned above in a logon script, it will not be available to the user for anything else. This is normal and completely expected if you understand how things work.

SO…. how do you do it? Well with a tool that sets persistent environment variables of course. The tool from MS for this is called SETX. It is available here

http://www.microsoft.com/windows2000/techinfo/reskit/tools/existing/setx-o.asp

That program actually writes the environment variable to the registry so that the next time a process is spawned, the registry is read and that populates the environment variables for the user.

If you want, you can set a persistent variable by popping the registry directly instead. You want to go look at HKCU\Environment.

SETX will also set system level persistent environment variables as well which is handy. A system level persistent environment variable is one that is applied to all processes of all users. Again, you can pop the registry directly, these values are stored in HKLM\System\CurrentControlSet\Control\Session Manager\Environment.

Rating 3.00 out of 5

Comments are closed.

[joeware – never stop exploring… :) is proudly powered by WordPress.]