Set Environment variable from logon script

Posted on 1997-06-30
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.
Question by:gd015

Expert Comment

ID: 1561548
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

Author Comment

ID: 1561549
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.


Expert Comment

ID: 1561550
Let me se a part of your logon script, please.
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.


Expert Comment

ID: 1561551
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 ???


Author Comment

ID: 1561552
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?


Accepted Solution

gantriis earned 200 total points
ID: 1561553
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.


Expert Comment

ID: 1561554
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.


Author Comment

ID: 1561555
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.


Expert Comment

ID: 1561556
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.

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is in response to a question ( here at Experts Exchange. The Original Poster (OP) requires a utility that will accept a list of IP addresses …
We recently endured a series of broadcast storms that caused our ISP to shut us down for brief periods of time. After going through a multitude of tests, we determined that the issue was related to Intel NIC drivers on some new HP desktop computers …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question