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.

  • 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. Jul 27, 2009 at 16:59
  • Interesting. I thought for sure there would be a fairly well-known third-party tool for this. Jun 26, 2010 at 8:49
  • What version of Windows are you running? Jun 26, 2010 at 8:52
  • @musicfreak Vista Home Premium 64bit Aug 14, 2011 at 18:38

12 Answers 12


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.

  • 1
    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. Jun 26, 2010 at 12:13
  • If this is combined with Vi, it'd come closer to meeting the questioner's criteria. e.g. a batch file that writes the path to a temporary text file, opens it in vi, then when you close vi it writes the file to the path. That way you can easily edit your path via command line. I did something like that in XP, with a batch file, notepad and those reg commands.
    – barlop
    May 22, 2013 at 22:54
  • 1
    WARNING do NOT copy-paste this mindlessly unless you want to do something silly...
    – n611x007
    Jul 11, 2014 at 16:01
  • This approach would take a system restart to apply. The OS is not aware this value is changed (without triggering WM_SETTINGSCHANGE message). To avoid a reboot, use a tool similar to EnvUpdate of AutoHotkey autohotkey.com/docs/commands/EnvUpdate.htm, or use setx command to set and unset some unimportant value after setting the registry value (setx tool has its own limitations, so a combination of both may be better in some cases)
    – wandersick
    Jun 16, 2020 at 9:29

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%]

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.


I just discovered the ability to allow the users to run the Environment Variables edit dialog without elevated privileges.

From the Start menu, run the following:

rundll32 sysdm.cpl,EditEnvironmentVariables
  • 3
    +1 for a command line way of getting to the GUI edit widget, because that's certainly more straight forward than the click path, even though the answer is off-topic because Q is about editing PATH from command line. May 22, 2013 at 21:25

You might wanna check out pathed of the gtools collection: http://www.p-nand-q.com/gtools.html

It provides a set of commands for the command promt like

pathed /APPEND %CD% /USER

to append the current path for example. I haven't really checked it out to be honest, as I'm totally fine with using a GUI.

Other options are:

  /MACHINE: print machine PATH
     /USER: print user PATH
      /ADD: add variable at the head
   /APPEND: add variable at the tail
   /REMOVE: remove path / index
     /SLIM: strip duplicate vars
      /ENV: environment variable, defaults to PATH

Together with which of the same collection, you got yourself some good tools, I suppose. Which "locates executable files on the PATH".

 /EXTENSION: search for extension , can be a ; separated list
       /DIR: add directory , can be a ; separated list
 /RECURSIVE: search directories recursively
    /SINGLE: stop after the first find result
       /ENV: environment variable, defaults to PATH
FILE {FILE}: one or more files to find

Source: http://op111.net/82/

  • 1
    Can you explain what you're linking to? Link-only answers can become invalid if the linked content becomes unavailable.
    – bwDraco
    Feb 10, 2013 at 22:49
  • Shure. Sorry i was implictly refering to the question. Feb 11, 2013 at 0:31
  • Seems the URL for Gtools and pathed is now dead. p-nand-q.com/gtools.html
    – DevPlayer
    Jan 3, 2015 at 7:12

set PATH

(help set)

  • 1
    this is only for the terminal session, and to add/change values you need to retype the entire old path as well.
    – user1931
    Jul 27, 2009 at 17:02
  • 2
    Not true. SET PATH=%path%;c:\newpath Jul 27, 2009 at 17:03
  • 2
    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.
    – user1931
    Jul 27, 2009 at 17:07
  • He didn't specify whether it needed to persist between sessions.
    – sangretu
    Jul 27, 2009 at 18:26
  • 4
    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.
    – user1931
    Jul 27, 2009 at 18:32

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.


@echo off
set cwd=%~dps0

goto testit


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


if not _%1==_ goto loopy

call %cwd%lpath.bat

endlocal & set path=%path%


@echo off
goto START


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 and lovingly maintained since then.


setlocal enabledelayedExpansion
set rc=0
set cwd=%~dps0
set curdrive=%~d0
set tr=%curdrive%\bin\tr.exe
set regexe=%windir%\system32\reg.exe

if _%1==_ goto Usage

@REM convert arg 1 to a fully-qualified, short path name,
@REM and then convert to uppercase.
set toupper=%~fs1
call :ToUpper
set tocheck=%toupper%

if not _%TEMP%==_ goto GotTemp
call :gettemp

set d=%DATE:~4%
set stamp=%d:~6%%d:~3,2%%d:~0,2%%TIME::=%
set d=
set tempfile1=%TEMP%\chkpath1-%stamp%.tmp

echo %path% | %tr% ; \n  >  %tempfile1%

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

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

* checkelt
* check one element in the path to see if it is the same
* as the TOCHECK string. The element is first canonicalized.

@REM remove surrounding quotes
set ERF=%1
if [x%ERF%]==[x] goto CheckEltDone
@REM convert to fully-qualified, short paths, uppercase
set TOUPPER=%~fs1%
call :ToUpper
if _%TOCHECK% == _%TOUPPER% set rc=1

* backtick
* invoke a command and return the result as a string.
* This is like backtick in csh or bash.
* To call, set variable BACKTICK to the command to be run.
* The result will be stored in the env variable of the same name.

FOR /F "usebackq delims=" %%i IN (`%backtick%`) DO (
  SET backtick=%%i
goto backtick_done
  SET backtick=nothing to exec

* gettemp
* get the temporary directory, as stored in the registry.
* Relies on backtick.
* The result set TEMP.

set regkey=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
set regvalname=Local AppData
set backtick=%regexe% query "%regkey%" /v "%regvalname%"
call :backtick
for /f "tokens=4" %%a in ("%backtick%") do (
  set temp=%%a

* ToUpper
* Convert a string to all uppercase.
* To call, set variable TOUPPER to the thing to be converted.
* The result will be stored in the env variable of the same name.

  FOR /F "usebackq delims=" %%I IN (`echo %toupper% ^| %tr% a-z A-Z`) DO (
SET toupper=%%I

  if _%tempfile1%==_ goto CleanUpDone
  if exist %tempfile1% del %tempfile1%

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.
goto END

call :CleanUp


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


@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
  • I think the for loop could be used instead of tr/cat too. Jul 28, 2009 at 6:57

This is really easy, environment variables are stored in registry, specifically user environment variables are stored in HKEY_CURRENT_USER\Environment, system environment variables are stored in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.

You don't need any third party software, you can achieve this I recommend you to use PowerShell, you can use Get-ItemProperty to query registry values and use Set-ItemProperty to modify registry values, New-Item to add registry keys, Remove-Item to delete registry keys and Remove-ItemProperty to delete registry values.

Now for example, to edit the PATH environment variable, you can use these:

If you want to edit the user PATH, you can use $Env:PATH to get its value;

Otherwise use (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" -Name PATH).PATH to get value of system PATH.

Now this is a script example that modifies system PATH:

$directory=Read-Host "Please input directory that needs to be added to PATH"
$path=(Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).PATH.Split(';')
if ($directory -notin $path) {$path+=$directory}
$path=$path -join ";"
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Type String -Value $path

Save as a *.ps1 file and run it with PowerShell, it will add the directory you typed to PATH if it isn't already in PATH.


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.

  • maybe try setx.exe
    – n611x007
    Jul 11, 2014 at 16:00
  • @n611x007, setx is for general environment variables; pathman is specifically for manipulating (i.e., parsing) the path.
    – Synetech
    Mar 23, 2016 at 20:50

How to check if directory exists in %PATH%? on Stack Overflow has an outstanding description of what makes Windows PATH editing difficult along with a batch file to overcome them. Figuring out how to properly use addpath.bat took a bit of practice as it's calling structure was new to me, but this works:

set _path=C:\new\directory\to\add\to\path
call addpath.bat _path
set _path=

and repeated firings won't add the new directory if it's already present. This doesn't address making the edits persistent across sessions.


I realize this is a late answer, but there is also EditPath.exe available in the release package for PathMgr.dll.

EditPath.exe is a command-line tool you can use to update the system and user Path values in the registry. It can also test whether a directory exists in the system and user Path (determined by an exit code -- see the documentation).

Alternatively, you can use editenv if you want to interactively edit the value of an environment variable (such as Path) for the current process.


Not an expert when it comes to the coding stuff, but I have been using a program called "clink" as of recent. Its easy to learn and although its still not an automated, its much more efficient (to me) than the standard setx or GUI. If your familiar with Chocolatey (or "choco" in the console), its as easy as...

winkey + r ... cmd.exe ... choco upgrade clink (upgrade replaces install fyi)

Its a very simple syntax to learn, and almost makes setting your environment variables not a complete drag :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .