问题
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.
reg add HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment /v ToDelete /t REG_SZ /d "192.168.00.00"
Thanks
回答1:
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"
回答2:
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.
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).
来源:https://stackoverflow.com/questions/12854862/adding-environment-variable-from-command-prompt-batch-file