Adding directory to Systemvariables Path in CMD

122 views Asked by At

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?

1

There are 1 answers

0
lit On

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.