Problem with user level environment variables set from VBS logon script not being available until logoff/logon RRS feed

  • Question

  • Hi,

    I'm a little stuck with this problem on Windows 7. Our VBS logon script sets certain user level environment variables. On Windows XP and 2003 this works perfectly. On Windows 7, it does not.

    Details of the problem are posted in the Windows 7 forum here. I have had no luck getting a solution there so thought I'd try in here instead :)

    A quick overview of the problem is that when a user logs onto Windows 7 for the first time, the environment variables set by the logon script are not available until the user logs off and then on again.

    They are set using WScript.Shell.Environment like this:

    Set Shell = CreateObject("WScript.Shell")
    Set TEnv = Shell.Environment(TLevel)
    TEnv(TName) = TValue

    I have even tried putting this into an in-house VB.NET DLL that sets them like this:

    Environment.SetEnvironmentVariable(VarName, VarValue, EnvironmentVariableTarget.User)

    However they are set, they appear in the Registry and Computer -> Properties -> Advanced -> Environment Variables.

    They just won't work!

    Restarting explorer.exe and dwm.exe have no affect so at this stage I am guessing that this is a bug in winlogon.exe or some other parent process.

    So, I have 2 questions:

    1. Does anyone know how to force a refresh of user environment variables?
    2. Is this a bug - can someone from Microsoft comment please?

    Remember that this problem is only seen during a first logon (or the 1st time a new variable is set I think), when the variables are set during the logon.

    One last thing, if, after logon, I manually set a new variable, the others are then picked up, which suggests that there is a way to refresh environment variables for new processes...



    • Edited by Rob.Ford Tuesday, November 24, 2009 11:18 PM added bit more
    Tuesday, November 24, 2009 10:48 PM


  • If anyone else has this issue, here is how I solved it:

    1. Variables are set during logon using WScript.Shell.Environment as normal
    2. After logon (luckilly I have a small exe already running to add this to) I refresh the user environment variables using the following VB.NET code:

    Declare Function SendMessageTimeout _
    Lib "user32" Alias "SendMessageTimeoutA" _
    ByVal hwnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, _
    ByVal lParam As String, ByVal fuFlags As Integer, ByVal uTimeout As Integer, _
    ByRef lpdwResult As Integer) As Integer


    Const HWND_BROADCAST& = (-1)
    Dim rtnValue As Integer
    Dim result As Integer = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, _
    "Environment", SMTO_ABORTIFHUNG, 5000&, rtnValue)

    Thanks to TheLearnedOne at experts-exchange for that code.

    If this is run during/from the logon process, it does not work. It must be run after the logon process completes. Alternatively, you could create/delete a temp variable for the same result.

    Once this is run after logon, all environment variables set during logon are then available to new processes as you would expect.

    However, I still think this is a small bug in Windows 7, it would be nice if someone from Microsoft could pass this onto the relevant group, thanks.


    • Marked as answer by IamMred Tuesday, February 2, 2010 8:50 PM
    Wednesday, November 25, 2009 3:14 AM