Is there any command line tool that can be used to edit environment variables in Windows?

I don't know any tool that does this, but maybe you can use the regcommand:

reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path

to read the current path, and

reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /d "newPath" /f

to write your new value.

You need admin rights for hsving right acccess in HKLM. If that is a problem, consider modifying the user specific path setting in HKCU\Environment instead.


If you need a generic way to set any environment variable and have the changes persist, then setx.exe would be the tool to use. It cannot do the "smart" things you are asking for, though...

setx.exe is included with Windows Vista or later; if you use an earlier version of Windows, you can use the above download link to get it.


For the current program, there is path:

Displays or sets a search path for executable files.

PATH [[drive:]path[;...][;%PATH%]
PATH ;

Type PATH ; to clear all search-path settings and direct cmd.exe to search only in the current directory.

Type PATH without parameters to display the current path. Including %PATH% in the new path setting causes the old path to be appended to the new setting.

However, this is pretty much the same as set PATH.

For environment variables to persist you have to edit the registry or use setx.