up vote 8 down vote favorite
4
share [g+] share [fb]

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

It would be nice if this was smart tool, for example:

  • When adding some path to let's say the PATH variable and this path is already there it shouldn't double this entry.
  • Inserting a new path to the PATH variable should be possible before/after some other path or in specific order (the first, the 7th, the last etc.).
  • It should be possible to change only part of variable's value (in case of the PATH a given path from a list of all paths).

And the last but not the least - I want my changes to persist between sessions so simple SET is out of question...

There's very nice GUI tool for this called Path Editor and I need something like this but for command line.

link|improve this question

55% accept rate
4  
Technically, yes. It's called SET. I know it's not pretty, but you can't get much more straightforward than just setting the variables with the tools at hand. – Michael Todd Jul 27 '09 at 16:59
Interesting. I thought for sure there would be a fairly well-known third-party tool for this. – musicfreak Jun 26 '10 at 8:49
What version of Windows are you running? – musicfreak Jun 26 '10 at 8:52
@musicfreak Vista Home Premium 64bit – Piotr Dobrogost Aug 14 '11 at 18:38
feedback

6 Answers

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.

link|improve this answer
+1. That should work just fine. – Marcus Lindblom Aug 22 '09 at 7:59
This answer should have been accepted, it can change any environement variable and can even be encapsulated in a batch file so one only needs to enter the name and the new value. – Tom Wijsman Jun 26 '10 at 12:13
feedback

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.

link|improve this answer
feedback

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.

link|improve this answer
feedback

set PATH

(help set)

link|improve this answer
this is only for the terminal session, and to add/change values you need to retype the entire old path as well. – John T Jul 27 '09 at 17:02
2  
Not true. SET PATH=%path%;c:\newpath – Michael Todd Jul 27 '09 at 17:03
1  
you still typed it, just unexpanded :) That also won't save, try exiting the command prompt and opening a new one then check your path. – John T Jul 27 '09 at 17:07
He didn't specify whether it needed to persist between sessions. – sangretu Jul 27 '09 at 18:26
3  
Well if you checked out what patheditor (his suggestion) does, it saves it permanently. set PATH is only really used in build scripts for programs because it is temporary. – John T Jul 27 '09 at 18:32
show 1 more comment
feedback

I wrote a set of batch scripts for this. addpath.bat adds elements to the path, rmpath.bat removes elements from the path, and lpath.bat just lists the path. But then I needed some support scripts, so there is also chkpath.bat .

It ended up being not trivial and required tr.exe and cat.exe, a couple of unix-style utilities. The reason its not trivial: no backticks in cmd.exe (though you can use for loops for this), and short names versus long names.

addpath.bat:

@echo off
setlocal
set cwd=%~dps0

goto testit

:loopy

call %cwd%chkpath "%~1"
if %errorlevel%==2 (
  set path=%path%;%~1
)

shift

:testit
if not _%1==_ goto loopy


call %cwd%lpath.bat

endlocal & set path=%path%

ChkPath.bat:

@echo off
goto START

-------------------------------------------------------
chkpath.bat

checks path for existence of the given segment.
Returns 1 if present, 2 if not present, 0 if not checked.

The matching and checking complicated by case sensitivity and "short pathnames".

created sometime in 2003

-------------------------------------------------------

:START
setlocal
set rc=0
set cwd=%~dps0
set curdrive=%~d0

if _%1==_ goto Usage

if not _%TEMP%==_ goto GotTemp
set temp=c:\

:GotTemp
call %cwd%\setz %cwd%stamp.bat

set tempfile1=%TEMP%\chkpath1-%setz%.tmp
set tempfile2=%TEMP%\chkpath2-%setz%.tmp

echo %path% | %curdrive%\utils\tr.exe ; \n  >  %tempfile1%

@REM convert to fully-qualified, short path names
set tocheck=%~fs1

@REM convert to uppercase:
call :ToUpper %tocheck%
set tocheck=%setz%


@REM check each element in the path for the match:
for /f  "delims=^" %%I in (%tempfile1%) do call:CheckElt "%%I"

if %rc%==0 set rc=2
goto END


--------------------------------------------
@REM subroutine
:CheckElt
    @REM @echo off
    if %rc%==1 goto CheckEltDone
    @REM remove surrounding quotes
    set ERF=%1
    @REM if empty, then skip it
    if [x%ERF%]==[x] goto CheckEltDone
    @REM convert to fully-qualified, short paths, uppercase
    set ERF=%~fs1%
    call:ToUpper %ERF%
    set ERF=%setz%
    @if [%tocheck%]==[%erf%] set rc=1
    :CheckEltDone
goto:EOF
@REM end subroutine
--------------------------------------------

--------------------------------------------
@REM subroutine
:ToUpper
  @echo %1 | %curdrive%\utils\tr.exe a-z A-Z > %tempfile2%
  call %cwd%setz %curdrive%\utils\cat.exe %tempfile2%
goto:EOF
@REM end subroutine
--------------------------------------------

--------------------------------------------
@REM subroutine
:CleanUp
  if _%tempfile1%==_ goto CleanUpDone
  if exist %tempfile1% del %tempfile1%
  if _%tempfile2%==_ goto CleanUpDone
  if exist %tempfile2% del %tempfile2%
  :CleanUpDone
goto:EOF
--------------------------------------------


--------------------------------------------
:Usage
echo.
echo Usage: chkpath ^<path^>
echo checks if path element is included in path variable.
echo returns 1 if yes, 2 if no, 0 if not checked.
echo.
goto END
--------------------------------------------


:END
call:CleanUp

:ReallyEnd

endlocal & set errorlevel=%rc%
@REM set errorlevel=%rc%
@REM echo %errorlevel%

lpath.bat:

@echo.
@set curdrive=%~d0

@REM This form post-fixes a | at the end of each path element. Useful for debugging trailing spaces.
@REM @path | %curdrive%\cygwin\bin\sed.exe -e s/PATH=// -e 's/;/^|\n/g' -e 's/$/^|/g'

@REM This form shows bare path elements.
@REM @path | %curdrive%\cygwin\bin\sed.exe -e 's/PATH=//' -e 's/;/^\n/g'
@path | %curdrive%\utils\sed -e "s/PATH=//" | %curdrive%\utils\tr ; \n
@echo.
link|improve this answer
I think the for loop could be used instead of tr/cat too. – grawity Jul 28 '09 at 6:57
feedback
up vote 0 down vote accepted

Path Manager (pathman.exe) from Windows Server 2003 Resource Kit Tools is the closest match I could find. It was already available in NT Resource Kit.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.