在 Windows 中将目录添加到 PATH 环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9546324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding directory to PATH Environment Variable in Windows
提问by Netorica
I am trying to add C:\xampp\php
to my system PATH
environment variable in Windows.
我正在尝试在 Windows 中添加C:\xampp\php
到我的系统PATH
环境变量中。
I have already added it using the Environment Variables dialog box.
我已经使用环境变量对话框添加了它。
But when I type into my console:
但是当我输入控制台时:
C:\>path
it doesn't show the new C:\xampp\php
directory:
它不显示新C:\xampp\php
目录:
PATH=D:\Program Files\Autodesk\Maya2008\bin;C:\Ruby192\bin;C:\WINDOWS\system32;C:\WINDOWS;
C:\WINDOWS\System32\Wbem;C:\PROGRA~1\DISKEE~2\DISKEE~1\;c:\Program Files\Microsoft SQL
Server\Tools\binn\;C:\Program Files\QuickTime\QTSystem\;D:\Program Files\TortoiseSVN\bin
;D:\Program Files\Bazaar;C:\Program Files\Android\android-sdk\tools;D:\Program Files\
Microsoft Visual Studio\Common\Tools\WinNT;D:\Program Files\Microsoft Visual Studio\Common
\MSDev98\Bin;D:\Program Files\Microsoft Visual Studio\Common\Tools;D:\Program Files\
Microsoft Visual Studio\VC98\bin
I have two questions:
我有两个问题:
- Why did this happen? Is there something I did wrong?
- Also, how do I add directories to my
PATH
variable using the console (and programmatically, with a batch file)?
- 为什么会这样?我做错了什么吗?
- 另外,如何
PATH
使用控制台(并以编程方式使用批处理文件)将目录添加到我的变量中?
采纳答案by Hans Passant
This only modifies the registry. An existing process won't use these values. A new process will do so if it is started afterthis change anddoesn't inherit the old environment from its parent.
这只会修改注册表。现有流程不会使用这些值。如果新进程在此更改后启动并且不从其父进程继承旧环境,则该进程将这样做。
You didn't specify how you started the console session. Best way to ensure this is to exit the command shell and run it again. It should then inherit the updated PATH.
您没有指定如何启动控制台会话。确保这一点的最佳方法是退出命令外壳并再次运行它。然后它应该继承更新的 PATH。
回答by JimR
Option 1
选项1
After you change PATH
with the GUI, close and re-open the console window.
PATH
使用 GUI更改后,关闭并重新打开控制台窗口。
This works because only programs started after the change will see the new PATH
.
这是有效的,因为只有更改后启动的程序才会看到新的PATH
.
Option 2
选项 2
Execute this command in the command window you have open:
在您打开的命令窗口中执行此命令:
set PATH=%PATH%;C:\your\path\here\
This command appends C:\your\path\here\
to the current PATH
.
此命令附加C:\your\path\here\
到当前PATH
.
Breaking it down:
分解它:
set
– A command that changes cmd's environment variables only for the current cmd session; other programs and the system are unaffected.PATH=
– Signifies thatPATH
is the environment variable to be temporarily changed.%PATH%;C:\your\path\here\
– The%PATH%
part expands to the current value ofPATH
, and;C:\your\path\here\
is then concatenated to it. This becomes the newPATH
.
set
–只为当前 cmd 会话更改 cmd 环境变量的命令;其他程序和系统不受影响。PATH=
– 表示PATH
要临时更改的环境变量。%PATH%;C:\your\path\here\
– 该%PATH%
部分扩展到 的当前值PATH
,;C:\your\path\here\
然后连接到它。这成为新的PATH
.
回答by Nafscript
WARNING:This solution may be destructiveto your PATH, and the stability of your system. As a side effect, it will merge your user and system PATH, and truncate PATH to 1024 characters. The effect of this command is irreversible. Make a backup of PATH first. See the comments for more information.
Don't blindly copy-and-paste this. Use with caution.
警告:此解决方案可能会破坏您的 PATH 以及系统的稳定性。作为副作用,它会合并您的用户和系统 PATH,并将 PATH 截断为 1024 个字符。此命令的效果是不可逆的。首先备份PATH。有关更多信息,请参阅评论。
不要盲目地复制和粘贴这个。谨慎使用。
You can permanentlyadd a path to PATH
with the setx
command:
您可以使用以下命令永久添加路径:PATH
setx
setx /M path "%path%;C:\your\path\here\"
Remove the /M
flag if you want to set the user PATH
instead of the system PATH
.
/M
如果要设置用户PATH
而不是系统,请删除该标志PATH
。
Notes:
笔记:
- The
setx
command is only available in Windows 7 and later. You should run this command from an elevated command prompt.
If you only want to change it for the current session, use set.
- 该
setx
命令仅在 Windows 7 及更高版本中可用。 您应该从提升的命令提示符运行此命令。
如果您只想为当前会话更改它,请使用set。
回答by zar
You don't need any set
or setx
command, simply open the terminal and type:
您不需要任何set
或setx
命令,只需打开终端并输入:
PATH
This shows the current value of PATH variable. Now you want to add directory to it? Simply type:
这显示了 PATH 变量的当前值。现在要向其中添加目录吗?只需键入:
PATH %PATH%;C:\xampp\php
If for any reason you want to clear the PATH variable (no paths at all or delete all paths in it), type:
如果出于任何原因要清除 PATH 变量(根本没有路径或删除其中的所有路径),请键入:
PATH ;
Update
更新
Like Danial Wilson noted in comment below, it sets the path only in current session. To set the path permanently use setx
but be aware, although that sets the path permanently but NOT in the current session, so you have to start a new command line to see the changes, more info here.
就像 Danial Wilson 在下面的评论中指出的那样,它仅在当前会话中设置路径。要永久设置路径,请setx
注意,虽然这会永久设置路径但不在当前会话中,因此您必须启动一个新的命令行以查看更改,更多信息请点击此处。
To check if an environmental variable exist or see its value use ECHO command:
要检查环境变量是否存在或查看其值,请使用 ECHO 命令:
echo %YOUR_ENV_VARIABLE%
回答by Ifedi Okonkwo
I would use PowerShell instead!
我会改用PowerShell!
To add a directory to PATH using PowerShell, do the following:
要使用 PowerShell 将目录添加到 PATH,请执行以下操作:
$PATH = [Environment]::GetEnvironmentVariable("PATH")
$xampp_path = "C:\xampp\php"
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path")
To set the variable for all users, machine-wide, the last line should be like:
要在机器范围内为所有用户设置变量,最后一行应如下所示:
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
In a PowerShell script, you might want to check for the presence of your C:\xampp\php
before adding to PATH (in case it has been previously added). You can wrap it in an if
conditional.
在 PowerShell 脚本中,您可能希望C:\xampp\php
在添加到 PATH 之前检查您的存在(以防之前已添加)。您可以将其包装在if
条件中。
So putting it all together:
所以把它们放在一起:
$PATH = [Environment]::GetEnvironmentVariable("PATH")
$xampp_path = "C:\xampp\php"
if( $PATH -notlike "*"+$xampp_path+"*" ){
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
}
Better still, one could create a generic function. Just supply the directory you wish to add:
更好的是,可以创建一个通用函数。只需提供您要添加的目录:
function AddTo-Path{
param(
[string]$Dir
)
if( !(Test-Path $Dir) ){
Write-warning "Supplied directory was not found!"
return
}
$PATH = [Environment]::GetEnvironmentVariable("PATH")
if( $PATH -notlike "*"+$Dir+"*" ){
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$Dir", "Machine")
}
}
You could make things better by doing some polishing. For example, using Test-Path
to confirm that your directory actually exists.
你可以通过做一些抛光来使事情变得更好。例如,Test-Path
用于确认您的目录确实存在。
回答by icc97
Safer SETX
更安全的 SETX
Nod to all the comments on the @Nafscript's initial SETX
answer.
对@Nafscript 的初始SETX
答案的所有评论表示赞同。
SETX
by default will update your userpath.SETX ... /M
will update your systempath.%PATH%
contains system path with user path appended
SETX
默认情况下将更新您的用户路径。SETX ... /M
将更新您的系统路径。%PATH%
包含附加了用户路径的系统路径
Warnings
警告
- Backup your
PATH
-SETX
will truncate your junk longer than 1024 characters - Don't call
SETX %PATH%;xxx
- adds system path into the user path - Don't call
SETX %PATH%;xxx /M
- adds user path into the system path - Excessive batch file use can cause blindness1
- 备份您的
PATH
-SETX
将截断超过 1024 个字符的垃圾 - 不要调用
SETX %PATH%;xxx
- 将系统路径添加到用户路径中 - 不要调用
SETX %PATH%;xxx /M
- 将用户路径添加到系统路径中 - 过度使用批处理文件会导致失明1
The ss64 SETX pagehas some very good examples. Importantly it points to where the registry keys are for SETX
vs SETX /M
该ss64 SETX页面有一些非常好的例子。重要的是它指向的注册表项是哪里SETX
VSSETX /M
User Variables:
HKCU\Environment
System Variables:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
用户变量:
HKCU\Environment
系统变量:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Usage instructions
使用说明
Append to User PATH
附加到用户 PATH
append_user_path.cmd
append_user_path.cmd
@ECHO OFF
REM usage: append_user_path "path"
SET Key="HKCU\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
ECHO %CurrPath% > user_path_bak.txt
SETX PATH "%CurrPath%";%1
Append to System PATH
附加到系统 PATH
append_system_path.cmd
. Must be run as admin.
append_system_path.cmd
. 必须以管理员身份运行。
(it's basically the same except with a different Key
and the SETX /M
modifier)
(除了Key
和SETX /M
修饰符不同外,基本相同)
@ECHO OFF
REM usage: append_system_path "path"
SET Key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
ECHO %CurrPath% > system_path_bak.txt
SETX PATH "%CurrPath%";%1 /M
Alternatives
备择方案
Finally there's potentially an improved version called SETENVrecommended by the ss64 SETX page that splits out setting the user or system environment variables.
最后,ss64 SETX 页面推荐了一个名为SETENV的改进版本,该版本可以拆分设置用户或系统环境变量。
1. Not strictly true
1. 不完全正确
回答by nclord
Late to the party - but handy if you are already in the directory you want to add to PATH.
迟到了 - 但如果您已经在要添加到 PATH 的目录中,则很方便。
set PATH=%PATH%;%CD%
set PATH=%PATH%;%CD%
edit: as per comment - works with standard windows cmd but not in powershell.
编辑:根据评论 - 适用于标准 Windows cmd,但不适用于 powershell。
For powershell the %CD%
equivalent is [System.Environment]::CurrentDirectory
对于powershell,%CD%
相当于[System.Environment]::CurrentDirectory
回答by Thomas Fonseca
What if you mistype the path using setx? The best way is simply through the windows U.I. Control Panel->All Control Panel Items->System->Advanced System Setttings->Environment Variables
如果您使用 setx 输错了路径怎么办?最好的方法就是通过windows UI 控制面板->所有控制面板项目->系统->高级系统设置->环境变量
Scroll down to Path and select Edit. You can also copy and paste it into your favorite editor so you can see the entire path and more easily edit it.
向下滚动到路径并选择编辑。您还可以将其复制并粘贴到您喜欢的编辑器中,以便您可以查看整个路径并更轻松地对其进行编辑。
回答by hevi
- command line changes will not be permanent, will be lost when the console closes.
- Path works like first comes first served.
- You may want to override other already included executables. For instance if you already have another version on your path and you want to add different version without making a permanent change on path you should put the directory at the beginning of the command.
- 命令行更改不会是永久性的,会在控制台关闭时丢失。
- Path 的工作原理类似于先到先得。
- 您可能希望覆盖其他已包含的可执行文件。例如,如果您的路径上已经有另一个版本,并且您想添加不同的版本而不对路径进行永久更改,则应将该目录放在命令的开头。
To override already included executables;
覆盖已经包含的可执行文件;
set PATH=C:\xampp\php;%PATH%;
设置 PATH=C:\xampp\php;%PATH%;
回答by Netorica
Aside from all the answers, if you want a nice GUI tool to edit your windows environment variables you can use Rapid Environment Editor
除了所有的答案,如果你想要一个不错的 GUI 工具来编辑你的 Windows 环境变量,你可以使用Rapid Environment Editor
try it! its safe to use and awesome!
尝试一下!它使用安全且很棒!