44

We can use setx as discussed here.

setx PATH "%PATH%;C:\Something\bin"

But this command can just make changed to user PATH variable not the system one.

How can we make a similar system wide command?

enter image description here

Community
  • 1
  • 1
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
  • 1
    possible duplicate of [How to persistently set a variable in Windows 7 from a batch file?](http://stackoverflow.com/questions/6523979/how-to-persistently-set-a-variable-in-windows-7-from-a-batch-file) – Joe Oct 22 '14 at 11:56

6 Answers6

47

Type setx /? to get basic command help. You'll easily discover:

/M                     Specifies that the variable should be set in
                       the system wide (HKEY_LOCAL_MACHINE)
                       environment. The default is to set the
                       variable under the HKEY_CURRENT_USER
                       environment.

You need to run this from an elevated command prompt. Right-click the cmd shortcut and select Run as Administrator.

E.g.

setx /M PATH "%PATH%;C:\Something\bin"

Caution:

We may destroy the current system's PATH variable. Make sure you backup its value before you modify it.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 3
    `pathman` is probably a better choice than `setx` as it is specifically designed to manipulate paths. In particular, using `setx` breaks paths with embedded environment variable references, and `pathman` doesn't (as far as I know). – Harry Johnston Jun 14 '14 at 23:38
  • 2
    It is pretty inevitable that *somebody* is going to go "wow, that edit box is awfully small and I can fix that!" Which is very, very intentional (yes, you too Intel). And has nothing whatsoever to do with setx. – Hans Passant Jun 14 '14 at 23:45
  • @HarryJohnston Please put your answer into a separate one to get reviewed and accepted if that shows a better solution. – Nam G VU Oct 22 '14 at 13:33
  • 6
    `setx` may truncate the value to 1024 characters. (At least that was its claim for me on `Windows 7 Enterprise x64 SP1`). – Zarepheth Jun 08 '17 at 16:05
  • 1
    to avoid truncation - use this answer https://stackoverflow.com/a/37304698/1762994 – Michal Tsadok Oct 12 '18 at 15:01
  • 1
    This is wrong. It's modifying the merged system-user "Path" value and setting that back as the system value. This makes a mess and has lost the carefully implemented use of `REG_SZ` registry values in terms of referencing other variables in "Path". (Variables that do not reference other variables are set as `REG_SZ` values. They're loaded prior to `REG_EXPAND_SZ` values when the environment is reloaded, so they're safe to use in "Path".) setx.exe is *not* sufficient to update `PATH` on its own. It must be combined with reg.exe, and the batch code to implement this is complicated. – Eryk Sun Feb 02 '20 at 15:02
16

From powershell

setx /M PATH "$($env:path);c:\program files\mynewprogram"
Nathan Julsrud
  • 181
  • 1
  • 3
4

Solution when dealing with a >1024 char path:

None of the other answers worked in my case, but using pathed did the trick. You can append to path as simply as this:

pathed /append C:\Path\To\Be\Added /machine

You can check if the edit happened correctly by running

pathed

PS: if you want to change the user's path instead use: pathed /append C:\Path\To\Be\Added /user and pathed /user to check if it went through correctly.

PPS: In order to be able to run pathed from terminal, you need to put the exe in a directory already on your path (or add a new directory to path, but then you you might need to open a new instance of cmd.exe in order for the new path to be recognized)

jlo
  • 2,157
  • 2
  • 17
  • 23
2

One problem with %PATH%, is it includes the user's path. If you don't mind Powershell, you can run the following

$p = [Environment]::GetEnvironmentVariable("PATH", [EnvironmentVariableTarget]::Machine);
[Environment]::SetEnvironmentVariable("PATH", $p + ";C:\MyPath", [EnvironmentVariableTarget]::Machine);
Clay Lenhart
  • 1,597
  • 15
  • 17
0

If you want to add some location to the PATH environment variable on user level, use the following on the command line:

setx PATH ^%PATH^%;"C:\Program Files\Something\bin"

Why the strange syntax? First, you do not want to expand the system PATH variable but keep it as a symbol, otherwise you will not participate in future additions to the system PATH variable. Therefore, you have to quote the % characters with ^.

If you use this in a command script, you have to use double %% instead of ^%.

The " encloses a string that contains spaces. If you do not have spaces, you can omit the quotes.

The added string has to follow directly without space so the whole thing forms a single argument to the setx command.

Achim
  • 49
  • 3
  • Although possibly the only `CMD` solution, using `setx` should be avoided as described in [this SO answer](https://stackoverflow.com/a/69239861/1147688). Use admn powershell with properly gotten machine vs user PATH's. – not2qubit Nov 13 '22 at 18:36
0

Please refer to Adding a directory to the PATH environment variable in Windows

append_user_path.cmd
append_system_path.cmd

- both work just fine

Taraskin
  • 561
  • 9
  • 15