0

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

2 Answers 2

3

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"
Sign up to request clarification or add additional context in comments.

Comments

3

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

3 Comments

No, it comes in all the newer Windows OSes. However in older versions it may need to be downloaded from MS. Example: for Windows XP you can get it in the Windows XP SP2 support tools.
is this possible to define a System variable with SetX command?
@ir-tech, yes the /M switch. Open a command prompt and do a SET /? for the full syntax details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.