Adding environment variable from command prompt / batch file

后端 未结 2 2015
庸人自扰
庸人自扰 2021-01-16 17:25

I am trying to add a environment variable (System) in my batch file. The below command says ERROR: Invalid syntax. Can some one help. For me it looks good.

r         


        
相关标签:
2条回答
  • 2021-01-16 18:09

    SetX.exe is probably the easiest way to do this in a batch file. It also feels a bit safer than directly writing to the Registry just to add an Environment Variable.

    SetX syntax page

    For your example the variable's:

    • Space = System
    • Name = ToDelete
    • Value = 192.168.00.00

    The SetX command line would be:

    SetX.exe ToDelete "192.168.00.00" /m
    

    Note that this CMD was executed as an administrator. Depending on your systems setup SetX may not run under your users permissions.

    SetX command line in CMD

    Another thing to remember about SetX is that the update does not happen to the Environment in the current CMD window. you have to either also run a Set for the variable (temporary) or close and reopen CMD to see the update.

    In most cases (in scripts) it means running both commands. Note below this is a standard CMD session (has been closed and reopened).

    Showing the Set in CMD

    0 讨论(0)
  • 2021-01-16 18:12

    I think you need to quote the registry key as it has spaces

    reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v ToDelete /t REG_SZ /d "192.168.00.00"
    
    0 讨论(0)
提交回复
热议问题