Prevent Windows System %PATH% from being prepended to user %PATH?

19

2

I know how to set system or user specific environment variables:

Windows Environment Variables

Now, the problem is that the PATH variable is treated such that the value you enter for the user PATH will be automatically appended to the system PATH and that will be the effective PATH variable.

That is, say I have

(SYSTEM) PATH=C:\Windows\System32;C:\Program Files\Foo\bin;...

and (USER) PATH=C:\Program Files\Bar\bin (note that there is not %PATH% in this value)

then the resulting environment variable for this user will be:

(effective) PATH=C:\Windows\System32;C:\Program Files\Foo\bin;...;C:\Program Files\Bar\bin

However, I would rather like that for a very specific OS user account the PATH environment variable should have the bar\bindirectory at the beginning of the PATH instead of at the end.

Is there a proper way to tell windows to completely override the PATH variable of a user with the value for that user instead of appending it to the system PATHvariable?

Note: Obviously, from a batch file, all this doesn't matter as you can set and tweak the env.vars as you like.

Martin

Posted 2011-12-12T16:08:22.863

Reputation: 2 055

Why not just ignore the USER section. Delete the USER Path variable and put everything into the SYSTEM Path variable, in the order you wish it to be. – Kevin Fegan – 2013-04-25T23:33:18.293

1@KevinFegan: Well, because some services on the machine depend on that SYSTEM PATH and this specific user account would better be served by having a different PATH. I know that there are a lot of workarounds, but I was interested whether there is anything out of the box / on the OS/registry level to achieve this. – Martin – 2013-04-26T07:58:40.370

Answers

3

If you only need this to work for command prompt sessions, create a profile/init batch file and configure it in the registry, per https://stackoverflow.com/questions/17404165/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%

G-Wiz

Posted 2011-12-12T16:08:22.863

Reputation: 581

0

I had the same problem and this worked for me:

You can get "user path variable" through:

>reg query HKCU\Environment /v path

The output (on my machine) is:

HKEY_CURRENT_USER\Environment
    path    REG_SZ    C:\Program Files (x86)\GnuWin32\bin

But then you have to parse this and use the relevant part.

I read the solution here here and tested on my machine.

user2442366

Posted 2011-12-12T16:08:22.863

Reputation: 109

-1

I don't know how to ignore system variable, but I know that if you use session variables (via SET command), they take precedence over other variables. So you can use SET PATH=C:\Program Files\Bar\bin instruction and your desired path will be the first part of the variable.

Here is a valid reference, and I report here a simple period from that page:

If variables with the same name are stored as both User and Machine Environment variables, the user variable will take precedence. If a Session variable is created that will take precedence over any User and/or Machine Environment variable with the same name.

Gabriele Bertolucci

Posted 2011-12-12T16:08:22.863

Reputation: 17

That’s a useful reference, but the first sentence that you quoted is an overgeneralization.  The PATH variable is an exception (special case) — the user variable *does not* take precedence over the system variable.  The fact that a “session variable” will take precedence over both user & system pre-set variables is true, but how do you set a session variable automatically for a user?  G-Wiz has answered that question, and, AFAICS, you haven’t added anything to that answer.

– Scott – 2018-09-01T16:01:17.457