0

Imagine the following environment variables:

System PATH = C:\Windows
Bob's User PATH = C:\Users\Bob

In a command prompt the PATH command returns C:\Windows;C:\Users\Bob

After running setx /m PATH "C:\Node;%PATH%"
System PATH = C:\Node;C:\Windows;C:\Users\Bob

In a new command prompt the PATH command returns C:\Node;C:\Windows;C:\Users\Bob;C:\Users\Bob

Another user, Alice, logs in.
Alice's User PATH = C:\Users\Alice

On a command prompt the PATH command returns C:\Node;C:\Windows;C:\Users\Bob;C:\Users\Alice

Bob has a duplicate path in his PATH variable, and Alice has Bob's paths in her PATH variable.

Is there a way to append to the System PATH without polluting it with the current user's PATH?

  • In Windows 7, you can look up the System Path with reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v path. If that still works on Windows 8, then use that to build the new value. (You'll have to figure out how to parse the output of reg query.) – Scott Mar 12 '15 at 6:38
  • @Scott I'd be happy to accept your comment as an answer if you post it as one and include a working example. – jordanbtucker Mar 12 '15 at 15:07
1

In Windows 7, you can look up the System Path with

reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v path

If that still works on Windows 8, then use that to build the new value.

You'll have to figure out how to parse the output of reg query.  Here's something that might work:

for /f "tokens=1,2*" %a in ('reg query "HKLM\…\Environment" /v path') do set currentValue=%c

where

  • the 'reg query "HKLM\…\Environment" /v path' string is the reg query command (given above) enclosed in single quotes.
  • a, currentValue, and c are variable names.  You can choose whatever variable names you want, with the constraints that the a and the c must be single letters, two letters apart (e.g., you could use n and p, or x and z).
  •                             for /f "options" %variable in ('command1') do command2
    runs command1, parses the output, assigns value(s) to the %variable(s) (%a, above; but see also below) and executes command2.
  • tokens=1,2* means that %a gets the first token (word) of each (remaining) line, %b gets the second word, and %c gets the rest of the line.

    • The first word is path (the value name).
    • The second word is REG_EXPAND_SZ (the value type).
    • The rest of the line is the value.

    (You could just use tokens=2* and then currentValue=%b.)

So, after executing the above, you should be able to do

setx PATH "C:\Node;%currentValue%" /m
  • If you do this in a script (a .BAT file), use %%a and %%c.
  • Be sure to test this with echo commands before you do it with setx.
  • The only issue I'm running into now is that SETX is truncating my variable to 1024 characters. Looks like Powershell is going to be a better way to go. – jordanbtucker Mar 12 '15 at 21:39
  • This is also working on Windows 8. – jordanbtucker Mar 12 '15 at 21:40
  • An external command will have a command-line length limit. Such commands ought to have a way to take parameters from a fike instead. – JDługosz Mar 13 '15 at 1:27
  • @jdlugosz: Ironically, setx does have a /f option that tells it to read from a file. Unfortunately, I can't figure out how to tell it that spaces are not delimiters, so you can't handle commonplace PATH values like Program Files. – Scott Mar 13 '15 at 5:16
  • Try quotes like on the command line? Well, it's been discussed before time and time again. – JDługosz Mar 13 '15 at 13:18
0

Don't put Bob's specific path in the System Path value that's used by everyone.

Read the old value of the system path, modify that, and set the new value. If cmd.exe (apparently the set command can create/modify/delete but not read) doesn't provide those features, put it in a program called by the batch file.

The system path value is found (and changed) via the Registry. I just search for the value I know it has to discover exactly where, the Google that key path to check what's been said about it.
(later: they are HKLM\System\CurrentControlSet\Control\Session Manager\Environment and HKCU\Environment)

Any more-advanced scripting language for Windows, or with Windows API support, can handle that. Or, a compiled program executable in your language of choice.

he CMD replacement shell Take Command has a @REGQUERY function:

echo %@REGQUERY["HKLM_64\System\CurrentControlSet\Control\Session Manager\Environment\Path"]

since it sounds like you have a specific machine in mind, you can install TCC/LE (free) and use that to interpret your batch file.

  • "Don't put Bob's specific path in the System Path value that's used by everyone." Duh. The OP is asking how to do that, and your "answer" does not answer that question. Telling the user to Google his problem is not an acceptable answer. ... ... ... ... ... ... P.S. Windows doesn't have a program called command.exe any more. – Scott Mar 12 '15 at 6:18
  • I didn't say Google the problem: I said in general one can find the needed Registry setting by searching the registry for the value you know it has, and then be sure to double-check to make sure people have not posted things like "don't do that!" or, having been armed with the correct exact name, will turn up correct syntax and usage for that particular setting. – JDługosz Mar 12 '15 at 7:55
  • @jdlugosz I'm finding it hard to understand a lot of what you're trying to say in your answer. Do you have a working example that doesn't require a third-party app? Powershell might be an option though. This is not for a specific computer. – jordanbtucker Mar 12 '15 at 15:13
  • I'm not familiar with Powershell, but that, vbscript, jscript, and any real language for Windows should have a way to read the Registry. Find the funcgion for reading a Registry string value, and use that with the argument illustrated. – JDługosz Mar 13 '15 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.