Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am setting a environment variable with setx(win7) in a cmd , that I started from a batch file, if I close the shell an open a new from batch file again and use Set command to check it, I do not get this new variable. I can see the variable via windows and regedit, had someone an Idea what I do wrong?

EG.

Batch:

Rem set some local variables

Set varableA="test"

...

Start /B cmd.exe /k

In the opened shell:

Setx -m variableB value --> set system envirtomet vartiable

Close shell an run batch again

In the new shell:

Set var

And thgerre is not variableB but in Windows an registry I can see it

share|improve this question
    
-1 you should paste the command you used so people can repeat what you did. That's the number one thing you've done wrong, is the way you asked this question. – barlop Sep 19 '13 at 12:01
    
now you've shown what you did i've removed the -1. I can see one thing wrong there. With setx and -m, the -m has to be at the end. – barlop Sep 21 '13 at 23:02
    
@barlop hmm just tested in win7 the -m seems to work even when not at the end – barlop Jul 9 at 10:26

Even if you now know what you did wrong. You should still say in your question. Your question is badly asked 'cos you're asking why it is that what you did didn't work and you didn't show exactly what you did. Paste the commands you did.

Here is how one would use setx. This works.

C:\>setx aaa rrr

SUCCESS: Specified value was saved.

Here is proof that it set-

C:\>@REG QUERY "HKCU\Environment"


HKEY_CURRENT_USER\Environment
    TEMP    REG_EXPAND_SZ    %USERPROFILE%\AppData\Local\Temp
    TMP    REG_EXPAND_SZ    %USERPROFILE%\AppData\Local\Temp
    aaa    REG_SZ    rrr

now if you do set(the command to view the environment variables), the value won't show up. But open a new cmd window and do set, and it shows. It is in the user env variable section. rather than the system one.

For the system environment variable, you need setx aaa 123 -m (i.e. -m at the end), and the location in the registry for system variables, in xp or 7, would be HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and you'd need the cmd prompt in administrative. And watch out with the path you should back it up setx pathbk "%path%" -m

I recommend writing a batch file to permanently set %uvar% and %mvar% to point to the registry locations for the environment variables

set mvar=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

setx mvar "%mvar%"

set uvar=HKCU\Environment

setx uvar "%uvar%"

then you can do

reg query %uvar%

reg query %mvar%

Note- setx can be a bit dangerous, writing something you didn't intend, so do set>a.a first so you have a backup.

related

http://stackoverflow.com/questions/1472722/how-to-remove-an-environment-variable-from-the-system-configuration-with-a-batch

share|improve this answer
    
I suppose it may be a reasonable habit to put the value in quotes regardless - the quotes won't go in the registry so won't show when doing set either. And that works for when the thing contains spaces. The quotes would just be for where the value is written using setx and setx (or the C library used by setx), will strip them out. So it's just so setx knows it's not more parameters. I suppose then setx uses argsv rather than getcommandline. In windows if it used getcommandline it'd get the quotes and leave them in. Know though that you would almost never want quotes stored in the value. – barlop Jul 9 at 10:33
    
i've never seen a practical case of wanting quotes in a value, and in a path they may even be damaging. Strangely if the environemnt variable contains a space then setx puts quotes around the value, that might be a bug, though 'fortunately' one would rarely have a case of an environment variable with a space. And you can always correct things anyway without setx, if a setx goes wrong. – barlop Jul 9 at 10:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .