0

I am setting a environment variable with setx in 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:

Rem set system environment variable
Setx -m variableB value

Close shell an run batch again

In the new shell:

Set var

And there is no variableB but in Windows and registry I can see it

3
  • -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
    Commented Sep 19, 2013 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
    Commented Sep 21, 2013 at 23:02
  • @barlop hmm just tested in win7 the -m seems to work even when not at the end
    – barlop
    Commented Jul 9, 2016 at 10:26

1 Answer 1

2

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.

Note also, if you want to use reg to do it, then besides reg query, if you do reg /? you see e.g. reg add and you can do reg add /?

related

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

and

https://stackoverflow.com/questions/13222724/command-line-to-remove-an-environment-variable-from-the-os-level-configuration

2
  • 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
    Commented Jul 9, 2016 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
    Commented Jul 9, 2016 at 10:38

You must log in to answer this question.

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