Set environment variable in vbscript that can be access in a login script

Posted on 2006-05-11
Last Modified: 2013-12-03

when i set a User  envir variable in vbscript which can be used in the same CMD

all is ok if i run another script (or in another cmd)

but how can use this env variable in my loginscript ?

     Set objShell = WScript.CreateObject("WScript.Shell")
     Set colSystemEnvVars = objShell.Environment("User")      
     Set val = qeuryID("XXXXXXXXX")
      if val <> "" then
      colSystemEnvVars("MYVAR") = "val"

     cscript set-env.vbs
     echo %MYVAR%

Question by:ingetic
    LVL 86

    Expert Comment

    The problem is that an environment variable that is set like that will only be valid during the liftime of your .vbs script. If you want it to persist, you'll need to set ist in the registry under HKEY_CURRENT_USER\Environment
    LVL 7

    Author Comment

    my sample script set definitivly the variable , this is not the problem

    but for next shells, next sessions, ... ... not in the same CMD or script
    LVL 86

    Accepted Solution

    >>but for next shells, next sessions, ... ... not in the same CMD or script

    As I wrote: "The problem is that an environment variable that is set like that will only be valid during the liftime of your .vbs script."

    After your script has exited, the value you have set is gone. If you want it to persist for "next shells, next sessions", you will need to set it in the registry.
    LVL 7

    Author Comment

    As i wrote, your wrong,

    after my script exit, i can see the environment variable in necxt sessions, even after a reboot, in  next shells..

    BUT NOT in current shell !!!
    LVL 41

    Assisted Solution

    There are quiet a few command-line utilities that allow you a save an enviromental variable beyond the scope of the current shell.

    I've used the SETX command from the WinXP Support Tools
    LVL 6

    Expert Comment

    I have also been looking for an easy solution to this problem and the current accepted solutions are wrong.  What the author asks for is clear.  Here is how I suggest doing this.

            wscript.echo "set MYVAR=Value1"
            wscript.echo "set MYVAR2=Value2"

         call cscript //Nologo set-env.vbs > %tempt%\setVars.bat
         call %TEMP%\setVars.bat
         echo %MYVAR%

    This will output to the console:



    Since the current scope of the cmd.exe running cannot be altered by either WMI, setx or the registry, the only way is to set the variable in the current environment.

         call cscript //Nologo set-env.vbs > %tempt%\setVars.bat

    This will redirect all output of set-env.vbs to a file named setVars.bat. Make sure all wscript.echo commands output valid batch code.  Using "call" is important in order to wait for the called script to finish before continuing the logon script.

         call %TEMP%\setVars.bat

    This executes in the current scope the batch file created from the output of set-env.vbs.

    The variables are then available for use.

    I hope this could help anyone in this situation.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now