Reputation: 75
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 works only until the CMD window has 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 problems?
Upvotes: 2
Views: 4762
Reputation: 36348
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:
If the per-user persistent variable is set, its contents will be copied into the per-machine persistent variable, which is likely to be inappropriate;
If the persistent variable references other environment variables, the references will be replaced with the current value of those variables. If the referenced variables change, the PATH will no longer follow those changes. (To be honest, though, most of the time this won't matter: the feature isn't commonly used.)
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
.
Upvotes: 3