Nam G VU
Nam G VU

Reputation: 35384

How to update system PATH variable permanently from cmd?

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

Upvotes: 45

Views: 108774

Answers (6)

Hans Passant
Hans Passant

Reputation: 941615

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.

Upvotes: 52

jlo
jlo

Reputation: 2269

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)

Upvotes: 4

Taraskin
Taraskin

Reputation: 792

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

Upvotes: 0

Achim
Achim

Reputation: 57

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.

Upvotes: 0

Clay Lenhart
Clay Lenhart

Reputation: 1606

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);

Upvotes: 2

Nathan Julsrud
Nathan Julsrud

Reputation: 191

From powershell

setx /M PATH "$($env:path);c:\program files\mynewprogram"

Upvotes: 17

Related Questions