将目录添加到CMD中的系统变量路径



Iam正试图通过CMD将目录永久添加到Path。当我尝试使用命令时:

setx路径"%路径%;C: \Program Files(x86(\chromedriver\chromedriver.exe">

它只将其保存到用户变量Path中。有没有办法通过CMD将其添加到系统变量路径中?

这是PowerShell代码。可能有一些方法可以用cmd-only reg.exe来实现,但我还没有走上这条路。如果您在受支持的Windows系统上,PowerShell将可用。

为此,您需要在插值(解析(之前检索当前变量值。为用户PATH变量执行此操作:

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

获取系统PATH变量:

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

一旦有了当前插值前的PATH变量值,就可以在使用Set-Itemsetx.exe之前对其进行更改。设置系统路径可能需要管理员权限,或者应该需要管理员权限。

最新更新