windowscmdpathenvironment-variables

Setting the PATH environment variable via cmd


I tried to add to the PATH environment variable ";C:\my_EXE" so I can run the programs I put there via cmd (Windows 8). I tried this command:

set PATH=%PATH%;C:\my_EXE

but it changed the PATH environment variable only until the CMD window was closed.

I searched on Google and I found this command:

setx PATH "%PATH%;C:\my_EXE"

that is supposed to set it forever, but it also only works until the CMD window is closed and it made something like this:

[new PATH]=[old PATH]X2

It appears only in a new cmd and not in system properties(!=cmd, there is the PATH with my new path and not X2)

Why does this happen? How can I set the PATH environment variable without any problems?


Solution

  • There are two persistent PATH variables, the per-machine variable and the per-user one. They get appended together to produce the actual environment variable. (Environment variables in the PATH are also expanded at this point.)

    That's why you're getting the path doubled up, because you've set the per-user persistent variable to include everything from the environment variable (which already includes everything from the per-machine persistent one).

    You can use setx with the /m parameter to set the per-machine persistent variable, but this isn't ideal:

    Instead, consider using pathman which is specifically designed to manipulate paths. You can get pathman.exe from the Windows Server 2003 Resource Kit Tools download.

    Note that both setx and pathman may hang if there are any unresponsive GUI applications running, even if the application window is hidden. The best way to minimize this risk is to reboot the machine immediately before running any script that uses setx or pathman.