We help IT Professionals succeed at work.

Set Environment variable from logon script

gd015 asked
Medium Priority
Last Modified: 2013-12-23
How do I set environment variables from a logon script.

The NT resource kit has a utility SETX to set environment variables.

I can't get this to work when running from a logon script.
The value is set in the registry but the previous value that was set during the last session is the one that is available.

Its almost like the notify windows API's doesn't work during logon.

There may be another way altogether of acheiving my objective so if anyone knows, I would be most grateful.
Watch Question

The CD-ROM version of Windows 95 includes a tool called Winset.exe. This tool is designed to set environment variables in Microsoft Windows NT login scripts, but you can also use it to set global environment variables in Windows 95. The syntax for Winset.exe is the same as for the SET command.
For example, to set a global TEMP environment variable within Windows 95, follow these steps:

    1.Copy the Winset.exe file from the Admin\Apptools\Envvars           folder on the Windows 95 CD-ROM to the Windows folder on your     hard disk.

    2.Click the Start button, and then click Run.

    3.Type the following line in the Open box, and then click OK:

             winset temp=c:\temp

Note that running the Winset.exe tool at a command prompt has no effect in the current instance of the command prompt. A new command prompt must be launched before the change is apparent.

For additional information, please see the Readme.txt file in the Admin\Apptools\Envvars folder on the Windows 95 CD-ROM.

hope this helps


Sorry, I think there is a misunderstanding.  I am running NT4 workstation, not 95.  My mistake.

The SETX utility does the same as winset, and only applies to new shells also.

This is why my problem is so annoying, because when the logon finishes, and I start a new shell, the variable has not been updated.

Let me se a part of your logon script, please.

The User-Environment is stored in HKCU/Environment. You may use REGEDIT to set something there. The Global Environment must be somewher under HKLM and can only be changed by the Administrator.
The Problem is that you must tell the other programs that you changed the variable. Changing Registry is not sufficient.

From SETX description:
A command-line utility that offers a batch method for setting environmental variables in the the user or computer environment from a variety of sources, without any programming or scripting. Besides taking both the variable and value from the command line, it can also take values from Registry keys and offsets into text files. Without SETX, there is no method for settings values directly into the master environment for Windows NT. These are configureable only through Control Panel or through the Registry Editor. The SET command, which is internal to the command interpreter, sets variables only into the environment of the current window. SETX allows you to set values for variables in either the user or computer environment from one of three sources: Command Line Mode, Registry Mode, and File Mode.

Are you really sure that you used SETX and not SET ?
SETX is designed to do exactly that what you want.
Did you install SP3 ???


Ok try this.

With NT running open a command prompt.


close the shell and start another one.
enter SET |MORE and you will see

The setx utility works as intended and the next time the machine is booted, the variable remains set.

I have no problems here.

However, if you edit LOGON.BAT and include the command


and restart the following happens.

Run a command shell and type SET |MORE

You will see NEWVAR=BLAH  (The old setting)

If you open regedit and check the environment you will see NEWVAR set to NEWBLAH.  (SETX worked but the system wasn't notified)

If you remove the line from LOGON.BAT and logon again, then run SET |MORE , you will now see NEWVAR=NEWBLAH

Which brings me to the crux of the problem.
SETX does not work when used in a LOGON script.

How do I do it?

Hi gd015,

I have made the same test as you describe using the Windows NT resource kit utility SETX from a logon script to set or change an environment variable on the Windows NT workstation I log on to and it works just fine for me.

However, as brauner pointed out for the winset utility used under Windows 95, the change will only be effective in new command prompts being launched.

Also, if you launch the new command prompt from the logon script (e.g. by "start cmd" in the script) then the new prompt will inherit the environment from the instance running the logon script (where SETX will not set the environment variable). If you need the variable to be available there add a "SET varname=varstring" to the logon script.

If you launch the new command prompt where the variable is needed from the startup group you would probably need to specify the registry value RunLogonScriptSync as 1 in order to prevent the prompt being launched before the logon script has executed. See details for doing this in article Q131503: "How to Delay Start of Program Manager For Logon Scripts" (information still valid for NT 4.0) in the Microsoft knowledge base. If the variable needs to be available in an application or a command prompt it is important that the script has processed the SETX command before the application/prompt is launched (as this is the time where the environment is set).

This will also be a problem if you launch the new command enterpreter from an application that:
1. Was started before the SETX statement executed in your script.
2. Does not handle the environment change notification.

I hope this answers your question. If not, I think you will need to provide a little more information on the timing between the logon script and the new command prompt as well as information on how the new prompt in which you expect the change to appear is being launched.


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Yes this may work:

Edit or add value (REG_DWORD):

HKEY_CURRENT_USER\software\microsoft\windows nt\currentversion\winlogon

value: RunLogonScriptSync

0 = Don't wait for the logon script to complete before loading the desktop.
1 = Wait for the logon script to complete before loading the desktop.


OK, I think we have an answer, although not what I was hoping for.

Gantrii tried using setx and it worked for him.

I had tried all the above answers such as running scripts synchronously etc.

My logon script used a CALL statement to run some commands in another bat file.  It so happened that the call'ed bat file contained the setx commands.

Note that the bat file was CALLed, not STARTed.

If I moved the setx commands back to LOGON.BAT they worked.

I believe this to be some kind of obscure bug in NT.  If you run a command prompt, and then start another prompt from this shell, and then run setx, it still works OK.

So it looks like I am stuck with setting vars in LOGON.BAT for the time being.  Not a great solution when you have a complex procedure to code.

Thanks for everyone's help and comments.


GD015: next time please word your question better and give the details IN ADVANCE! Now we have an answered question where nobody can contribute and you do not have a solution.
Why grade if the answer is not satisfying? Why grade it "A" ?
All the answers and comment doesn't contain more information than given in the resourcekit.

I think your problem is NOT a bug, but unavoidable.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.