Windows – How to append a registry key value that has spaces in it

command linewindowswindows-registry

I found the following page (How do I modify the data of an existing registry key value name from cmd?) and it was extermely helpful, but I have a further question.

My PATH has a value that includes "c:\Program Files\Microsoft SQL Server\100\Tools\Binn\", and I'd like to add "P:\SQL" to it. When I run the script below, my PATH will then look like "C:\Program;P:\SQL". What needs to be modified so my PATH will look like "c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;P:\SQL"?

Here is what I have:

for /F "skip=2 tokens=3" %%r in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set oldVal=%%r    
echo previous=%oldVal%    
set newVal=%oldVal%;P:SQL    
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d %newVal% /f

Thanks.

Best Answer

How do I add a value to my PATH?

My PATH has a value that includes "c:\Program Files\Microsoft SQL Server\100\Tools\Binn\", and I'd like to add "P:\SQL" to it

Why are you reading/writing the registry?

Just use setx to add a value to your PATH:

setx PATH "%PATH%;P:\SQL" /m

Note:

  • /m Set the variable in the system environment HKLM.

    (The default is the local environment HKCU)


Further Reading


Related Question