0

Iam trying to add a directory permanently to Path via CMD. When i try to use the command:

setx path "%path%;C:\Program Files (x86)\chromedriver\chromedriver.exe"

it only saves it to the uservariables Path. Is there a way to add it to the systemvariables Path by using CMD?

LordB
  • 13
  • 1
  • 3
  • 5
    Please listen to this very carefully. **Do not ever use `setx` to modify the content of `%Path%`**. `%Path%` is not a standard variable, _(it holds the values of both the System and User `%Path%`'s)_, and by doing so, you will have corrupted it. If you want to add something to the System environment Path variable value, use the registry key, `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"` and make your modification to the `Path` value there. Also be aware that changes to that value cannot take effect within the same cmd instance either. – Compo Dec 15 '21 at 13:00
  • 3
    You should before you do that, of course, ensure that your `%Path%` is not now corrupted. Open a Command Prompt window, type `Path` and press the `[ENTER]` key. Check the output, to see if you've corrupted it, by having duplicates. If you have, which I'd suspect is now the case, you need to remove those duplicated entries, preferably from within the GUI. Just to clarify, there's no problem making a `setx` change to the variable Path in the System environment, _(by using the `/M` option)_, however, you cannot include the variable `%Path%` in the value string, bacause it's that which holds both. – Compo Dec 15 '21 at 13:06

1 Answers1

-1

This is PowerShell code. There is probably some way to do it with a cmd-only reg.exe, but I have not been down that path. If you are on a supported Windows system, PowerShell will be available.

To do this, you will want to retrieve the current variable values before interpolation (resolving). To do that for the user PATH variable:

(Get-Item -Path 'HKCU:\Environment').GetValue(
    'PATH',  # the registry-value name
    $null,   # the default value to return if no such value exists.
  'DoNotExpandEnvironmentNames' # the option that suppresses expansion
)

To get the system PATH variable:

(Get-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment').GetValue(
  'PATH',  # the registry-value name
  $null,   # the default value to return if no such value exists.
  'DoNotExpandEnvironmentNames' # the option that suppresses expansion
)

Once you have the current, pre-interpolation PATH variable value, it can be changed before using Set-Item or setx.exe. Setting the system path will probably require Administrator permission, or should.

lit
  • 14,456
  • 10
  • 65
  • 119