5

In windows system, the order of the different paths in PATH variable environnement defines the resolution priority :

Example:

  • If the path is constituted of the following : C:/Dir1;C/Dir2
  • And containing C:/Dir1/test.exe and C:/Dir2/test.exe
  • Running test.exe in cmd will resolve into C:/Dir1/test.exe

That's fine, but in Windows there is two PATH, the user one and the system one. And there are concatenated as following : <system-path>;<user-path>

So, the system path seems to always have priority over the user path.

Am I wrong ?

Regards,

testtest8
  • 61
  • 2
  • What are you really trying to do? – selbie Jan 27 '20 at 10:23
  • I trying to only modify the user path (so without admin privilege) (let's say adding `C:/Dir2`), but if the system path contains a path that's already resolve `test.exe` (let's say `C:/Dir1`), so the `C:/Dir2` will not resolve `test.exe`. (Assuming `test.exe` is in both `C:/Dir1` and `C:/Dir2`) – testtest8 Jan 27 '20 at 10:37

1 Answers1

1

If you are using PowerShell

The function GetEnvironmentVariable can get user's and system's variables separately. So you can add following settings to your $PROFILE, which is usually $env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:

$env:PATH = [Environment]::GetEnvironmentVariable("Path", "User") + ';' + [Environment]::GetEnvironmentVariable("Path", "Machine")

Or if you want to prepend some paths to current $env:PATH, instead of moving whole user's paths before system's, you can just add:

$env:PATH = 'C:\Dir1;' + $env:PATH # note there's a semicolon

For Command Prompt

Refer to this answer:

If you only need this to work for command prompt sessions, create a profile/init batch file and configure it in the registry, per How to run a command on command prompt startup in Windows. E.g.,

reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun ^
 /t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f

Then simply make modifications to the PATH in that batch file. E.g.,

SET USER_PATH=c:\whatever
SET PATH=%USER_PATH%;%PATH%
Xuesong Peng
  • 153
  • 2
  • 8