注本文为 “Windows 环境变量操作” 相关合辑。英文引文机翻未校。中文引文略作重排。如有内容异常请看原文。refresh environment variables without reboot windows 10无需重启 Windows 10 即可刷新环境变量Posted on 2018-08-22 | In win10Post author: killfundescription: Add a windows environment varibale without rebooting the computer说明无需重启计算机即可添加 Windows 环境变量process to refresh environment variables without reboot windows无需重启 Windows 即可刷新环境变量的操作步骤open cmd commend prompt window打开命令提示符窗口inputset PATHC- this will refresh the environment variables输入set PATHC该命令将刷新环境变量close and restart cmd window关闭并重新打开命令提示符窗口inputecho %PATH%to test输入echo %PATH%进行测试Refresh Environment Variables in Windows 11 (Without Reboot)在 Windows 11 中无需重启即可刷新环境变量Overview – Environment Variables in Windows概述——Windows 中的环境变量It seems that you’re learning to program and trying to refresh environment variables in Windows 11 without rebooting your computer and restarting your project and disrupting the workflow.你可能正在学习编程希望在 Windows 11 中无需重启计算机、无需重启项目或中断工作流程即可刷新环境变量。The Windows OS provides a way for system administrators to change the values of environment variables in the registry. A special section of the registry called “Environment” contains all user-defined environment variables. You can modify these values through the use of the “Regedit” application.Windows 操作系统为系统管理员提供了在注册表中修改环境变量值的途径。注册表中名为“Environment”的特定分区存储了所有用户自定义的环境变量可通过注册表编辑器Regedit修改这些数值。An environment variable is a named object that contains data used by one or more applications. The environment variables are stored in the registry, where they can be viewed and edited using standard tools.环境变量是包含供一个或多个应用程序使用的数据的命名对象其存储于注册表中可通过系统标准工具查看与编辑。By the end of this article, you’ll be able to refresh environment variables in Windows without rebooting your computer:阅读本文后你将能够通过以下方式在 Windows 中无需重启计算机即可刷新环境变量Using Chocolatey Package Manager (Recommended)使用 Chocolatey 包管理器推荐Using Command Prompt使用命令提示符What are Environment Variables in WindowsWindows 中的环境变量定义Variables are used to store many sorts of data. System settings, inventory statuses, and other data can be stored in these variables. Environment Variables are a type of variable that contains environmental information about a computer. The environment variables in Windows can be used to make changes to the system without having to edit the registry.变量用于存储各类数据系统设置、状态信息等均可存入变量。环境变量是一类包含计算机环境信息的变量在 Windows 中可通过环境变量调整系统配置无需直接编辑注册表。The variables in the environment are used to define variables for a variety of purposes. Windows itself does not require them. You put up these variables, and they determine how Windows runs it. You can use them, for example, to identify your user or its location.环境变量可用于定义多种用途的参数Windows 系统本身不强制要求这些变量由用户自行配置其会影响系统运行方式例如用于标识用户身份或路径位置。Environment Variables in Windows help you to assign values that will be used during the process of execution of any command on your computer. This will allow these commands to execute on a given set of values, altering their appearance.Windows 环境变量可用于设定计算机执行各类命令时所用的参数值使命令基于指定参数运行改变其运行表现。1. Refresh Environment Variables via Chocolatey Package Manager (Recommended)通过 Chocolatey 包管理器刷新环境变量Step 1:Open the Command Prompt from the Start menu, copy and paste the following command to install chocolatey package manager on your PC:步骤 1在开始菜单中打开命令提示符复制并粘贴以下命令以在计算机上安装 Chocolatey 包管理器”%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe” -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command “[System.Net.ServicePointManager]::SecurityProtocol 3072; iex ((New-Object System.Net.WebClient).DownloadString(‘https://community.chocolatey.org/install.ps1’))” SET “PATH%PATH%;%ALLUSERSPROFILE%\chocolatey\bin”Command to install Chocolatey using CMD通过命令提示符安装 Chocolatey 的命令Step 2:After installing the Chocolatey Package Manager, type the command “refreshenv” (without quotation marks) to restart the Command Prompt.安装 Chocolatey 包管理器后输入命令refreshenv不含引号以刷新系统变量。Refresh environment variables using chocolatey通过 Chocolatey 刷新环境变量NOTE:After the installation of the chocolatey package manager, every time you type and execute the “refreshenv” command, the environment variables will be refreshed immediately, without the need to restart the computer.安装 Chocolatey 包管理器后每次输入并执行refreshenv命令环境变量将立即刷新无需重启计算机。2. Refresh Environment Variables via Command Prompt (CMD)2. 通过命令提示符CMD刷新环境变量Step 1:In the Start menu, search forCommand Promptand run it as an administrator.步骤 1在开始菜单中搜索命令提示符并以管理员身份运行。Step 2:Type the command: “set PATH c” (without quotation marks), press the enter key,and restart the Command Prompt.输入命令set PATH c不含引号按下回车键随后重启命令提示符。“set PATH c” commandset PATH c命令Step 3:Type the command: “echo %PATH%” (without quotation marks) and press the enter key to check / verify system variables.输入命令echo %PATH%不含引号并按下回车键通过命令提示符检查 / 验证系统变量。“echo %Path%” commandecho %Path%命令NOTE:After installing the PostgreSQL (PgAdmin 4), I tried this way to update environment variables, and it worked nicely.注意安装 PostgreSQLPgAdmin 4后笔者使用该方法更新环境变量效果良好。FAQs Related to Environment Variables环境变量相关常见问题What is the difference between System variables and Environment variables?系统变量与用户环境变量的区别System environment variables are globally accessed by all users. The user Environment variables are specific only to the currently logged-in user and remain the default for the other users.系统环境变量对所有用户全局生效用户环境变量仅对当前登录用户生效其他用户沿用默认配置。Where are the system variables stored in the registry?系统变量在注册表中的存储位置The actual location of system variables in the registry is:HKEY_LOCAL_MACHINE SYSTEM CurrentControlSet Control Session Manager Environment.系统变量在注册表中的实际路径为**HKEY_LOCAL_MACHINE SYSTEM CurrentControlSet Control Session Manager Environment**。Are environment variables stored in memory?环境变量是否存储于内存中Environment variables and command line parameters are kept together at the top of the process memory structure, above the layer.环境变量与命令行参数共同存储于进程内存结构的顶部区域。Next Steps后续操作When you change the value of an environment variable in windows, the changes are not applied to your current windows session. You have to either log out and log in again or restart your device. But, in this article, we’ve covered how to reset environment variables in Windows 11 without rebooting the computer.在 Windows 中修改环境变量数值后更改不会立即应用于当前会话通常需要注销重登录或重启设备。本文介绍了在 Windows 11 中无需重启计算机即可重置环境变量的方法。Variables can be accessed through DOS commands by typing “set”. They can also be accessed through other programs such as PowerShell and Command Prompt using the Control Panel applet or any other number of available applications for manipulating environment variables.可通过 DOS 命令set查看变量也可通过控制面板、PowerShell、命令提示符等工具或其他环境变量管理程序进行操作。It’s important to know that the changes you make in the environment variables will be modified for other users of the system as well. They should not, however, be confused with Environment Variables in Windows.需注意环境变量的修改会对系统其他用户产生影响同时需区分系统变量与用户环境变量的概念。How do you add a Windows environment variable without rebooting?如何在不重启的情况下添加 Windows 环境变量I would like to add an Environment variable to a Windows machine (desktop or server) and be able to use it without rebooting that machine.想要在 Windows 计算机桌面版或服务器版中添加环境变量且无需重启该计算机即可使用。Say you have a production server which hosts a variety of apps and a new app needs a particular Environment variable to run. You do not want to reboot it while users are connected to your other apps. What choices do you have? I don’t like the wait-until-a-good-time-to-reboot option. There must be a better way. What am I missing?假设你有一台运行多个应用的生产服务器新应用需要特定环境变量才能运行。在其他应用仍有用户连接时你不希望重启服务器。你有哪些可选方案不希望等待合适时机再重启应当存在更优方法此处存在哪些未注意到的内容edited Nov 8, 2018 at 23:14YakovLasked May 16, 2009 at 4:09Chad Braun-DuinI had this same issue. I read somewhere that killing the explorer.exe process would update the variables and it worked. Then I just had to run explorer from the task manager.曾遇到相同问题。有资料显示结束 explorer.exe 进程可更新变量该方法有效之后只需在任务管理器中重新运行 explorer 即可。– user19694Commented Sep 8, 2009 at 20:39You have to close the command prompt, and reopen it again, for your path variables to update. The variables are loaded when cmd starts.需关闭并重新打开命令提示符路径变量才会更新。变量会在 cmd 启动时加载。– NiLInfiniteCommented Oct 20, 2014 at 13:30Opening a newcmdusing the task manager or explorer window does not work (at least with Win10), but using thestart menu and typingcmdand then checking the variable works.通过任务管理器或资源管理器窗口新建cmd窗口无效至少在 Win 10 中如此而通过开始菜单输入cmd后再查看变量则有效。– Andreas CovidiotCommented Mar 3, 2020 at 9:21Open the Task Manager, find there the Explorer process, restart it using the corresponding context menu item.打开任务管理器找到 Explorer 进程通过右键菜单中的对应选项重启该进程。– Ilya LysenkoCommented Mar 15, 2020 at 20:29AnswersChanges to environment variables should take effect immediately, if you make the change via the main Properties dialog for the computer in question (go to My Computer | Properties | Advanced | Environment Variables). After the changes are saved, Explorer broadcasts aWM_SETTINGCHANGEmessage to all windows to inform them of the change. Any programs spawned via Explorer after this should get the updated environment, although already-running programs will not, unless they handle the setting change message.若通过计算机属性对话框此电脑 → 属性 → 高级 → 环境变量修改环境变量修改内容应立即生效。保存修改后资源管理器会向所有窗口发送WM_SETTINGCHANGE消息以通知变更。此后通过资源管理器启动的程序会获取更新后的环境变量而已运行的程序则不会除非程序可处理该设置变更消息。I’m not able to tell from your problem description what specific problem you’re having with this. Can you tell us more about the specific scenario that isn’t working?从问题描述中无法判断具体遇到的问题能否补充未生效的具体场景This KB article may also be of use: How to propagate environment variables to the system以下知识库文章可能有参考价值如何向系统传播环境变量edited Jun 1, 2020 at 15:11user483906answered May 16, 2009 at 4:16CharlieOk, I guess I must have seen this behavior on a running service or something. I added a new environment variable using the method described above. Then, I was able to see the value after opening up a new command prompt and using the commandecho %myvar%. Thank you both for your answers.推测该现象出现在已运行的服务等场景中。使用上述方法添加新环境变量后新建命令提示符窗口并执行echo %myvar%命令即可查看变量值。感谢两位的解答。– Chad Braun-DuinCommented May 21, 2009 at 1:09For PowerShell users this snippet may be of help: poshcode.org/2049PowerShell 用户可参考以下代码片段。– Anders ZommarinCommented Oct 12, 2011 at 6:32If using cmd, you need to restart it if change env variable若使用 cmd修改环境变量后需重启该程序。– Neil McGuiganCommented Dec 13, 2012 at 5:08Just installed Scala on my PC but the PATH did not update; presumably the MSI did not broadcast the WM_SETTINGCHANGE message. I added a dummy SYSTEM variable i.e. CHANGE_TO_UPDATEz, that I update to trigger the WM_SETTINGCHANGE message - problem solved在电脑中安装 Scala 后 PATH 未更新推测是安装程序未发送 WM_SETTINGCHANGE 消息。添加临时系统变量 CHANGE_TO_UPDATEz 并修改该变量以触发消息发送问题得以解决。– Darren BishopCommented Jan 19, 2015 at 10:12In a command prompt type:runas /user:yourusernameyourdomain cmd在命令提示符中输入runas /user:你的用户名你的域 cmdIt will open up a new cmd prompt, then type:taskkill /f /im explorer.exe程序会打开新的命令提示符窗口随后输入taskkill /f /im explorer.exeThen type:explorer.exe接着输入explorer.exeNow after closing all command prompts, you will see that thePATHvariable has been truly updated.关闭所有命令提示符窗口后PATH变量即完成实际更新。All command prompts must be closed. Reopen a new command prompt, type path and you will see the new data.必须关闭所有命令提示符窗口重新打开后输入 path 命令即可查看更新后的数据。edited Dec 7, 2015 at 16:39Kara Marfiaanswered Mar 20, 2013 at 6:36Ron AndKim StengelOut of all the answers on this page, this is the only one that actually worked for me. Thank you!!本页所有回答中仅该方法有效十分感谢– coredumperrorCommented Dec 6, 2013 at 7:34This worked for me. I think the problem is that if you run cmd through explorer (to save you having to type in long paths) then explorer is never closed, even if you close all explorer windows. Thanks for the solution :该方法有效。问题原因应为通过资源管理器运行 cmd 时即便关闭所有资源管理器窗口相关进程仍未退出。感谢该解决方案。– Steve McCommented Sep 18, 2014 at 15:48Uhh. Please don’t kill windows explorer unless one of its processes are hung. Instead, open the shutdown windows dialog and cancel it while holdingctrlaltshift. That will cleanly exit windows explorer. In Vista the shutdown windows dialog is harder to find (but still present, at least through 7 (unsure about 8 and 10)), so there is a second method. ctrlshiftright-click in an empty part of the start menu and choose exit explorer. In 8 the exit option is the same, but you use the taskbar not the start menu.除非资源管理器进程卡死否则不建议强制结束该进程。可打开关机对话框按住ctrlaltshift并取消操作即可正常退出资源管理器。Vista 及更高版本系统中关机对话框较难找到至少 Win 7 仍存在Win 8 与 10 情况未知因此存在第二种方法在开始菜单空白处按住 ctrlshift 并右键单击选择退出资源管理器。Win 8 中操作相同仅需在任务栏而非开始菜单执行该操作。– Kevin CathcartCommented Aug 21, 2015 at 20:141 This works as a charm on Windows 7. FYI, I simply used the CTRLALTSHIFT and from theWindows Task Manager I killed all explorer.exe processand then started it again by hitting theNew Taskbutton.该方法在 Windows 7 中效果极佳。实际操作时按住 CTRLALTSHIFT在任务管理器中结束所有 explorer.exe 进程再通过新建任务按钮重新启动即可。– Marco DemaioCommented Nov 29, 2018 at 15:252020 / 2021 update for restartingexplorer.exe: Open up Task Manager (Ctrl Shift Esc). FindExplorer.exein the list of active processes in theProcessestab. Right-click and selectRestart.2020/2021 年重启explorer.exe的更新方法打开任务管理器Ctrl Shift Esc在进程选项卡的活动进程列表中找到Explorer.exe右键单击并选择重启。– rkeetCommented Feb 15, 2021 at 9:46One thing to keep in mind is that many programs obtain the environmental variables when they’re first started, so while windows may not need a restart, some programs might before they’ll be able to use the new variables. Good example of this is having to open a new command prompt window after adding a PATH (yes, I’ve been tripped up by this).需注意多数程序会在首次启动时获取环境变量。因此系统无需重启但部分程序需重启才能使用新变量。典型案例为添加 PATH 后需新建命令提示符窗口该场景曾造成使用困扰。answered May 16, 2009 at 6:11nedmThis problems affects ALL SERVICES, even a restarted service will not see new environment variables.该问题影响所有服务即便重启服务也无法识别新环境变量。– gimelCommented May 16, 2009 at 10:39Are you sure it’s not because of sharing a svchost process?确定该现象并非由共享 svchost 进程导致– Mark SowulCommented Nov 26, 2013 at 22:5720Whilst I don’t have enough of a reputation to comment on the highest voted answer to this question, I would like to state that it is not exactly correct. I know this because no matter which workaround I tried in this post, nothing actually worked.虽无权限对最高赞回答发表评论但需说明该回答不完全准确。尝试本文中所有方法后均未生效由此可得出该结论。The kb article linked to in that answer actually states that:该回答所附知识库文章实际内容如下However, note that modifications to the environment variables do not result in immediate change. For example, if you start another Command Prompt after making the changes, the environment variables will reflect the previous (not the current) values. The changes do not take effect until you log off and then log back on.需注意环境变量的修改不会立即生效。例如修改后新建命令提示符窗口变量仍显示修改前的值修改内容需在注销并重新登录后生效。The part about the environment variables resetting to the previous values after reloading the command prompt is exactly what I experienced in Windows Server 2008.重新打开命令提示符后变量恢复为旧值的现象与 Windows Server 2008 中的实际情况一致。The article goes on to say:文章后续内容如下To effect these changes without having to log off, broadcast a WM_SETTINGCHANGE message to all windows in the system, so that any interested applications (such as Windows Explorer, Program Manager, Task Manager, Control Panel, and so forth) can perform an update.若要在不注销的情况下使修改生效需向系统所有窗口发送 WM_SETTINGCHANGE 消息使相关应用如资源管理器、程序管理器、任务管理器、控制面板等执行更新操作。That does not imply that Explorer broadcasts a WM_SETTINGCHANGE message once you have changed the system environment variables, or that it actually works. I’m not sure how you would do what is suggested in the KB article (to propagate the changes immediately) from the command prompt.该内容不代表修改系统环境变量后资源管理器会自动发送 WM_SETTINGCHANGE 消息也不代表该操作一定有效。暂不清楚如何通过命令提示符执行文章中建议的立即传播修改操作。answered Nov 26, 2013 at 22:30testworksThis is the only answer that’s right for me. None of the others, including killing Explorer the hard way or the “proper” way, make any difference.仅该回答符合实际情况包括强制或正常结束 Explorer 在内的其他方法均无效。– Hashim AzizCommented Jan 15, 2019 at 19:33Windows 环境变量操作1 环境变量查看操作1.1 CMD 环境查看命令1.1.1 查看 PATH 变量echo %PATH%功能显示当前 CMD 窗口内 PATH 变量取值仅执行读取操作不改变变量状态。path功能专用 PATH 变量查看命令输出效果与echo %PATH%一致。1.1.2 查看全部环境变量set功能列出当前会话全部环境变量。1.1.3 查看指定环境变量set PATH set TEMP set USERNAME功能输出指定名称的环境变量取值。1.2 PowerShell 环境查看命令1.2.1 查看 PATH 变量$env:PATH1.2.2 查看全部环境变量Get-ChildItemEnv:1.2.3 查看指定环境变量$env:JAVA_HOME$env:USERNAME1.2.4 分离查看用户级与系统级 PATH当前用户 PATH[Environment]::GetEnvironmentVariable(PATH,User)系统全局 PATH[Environment]::GetEnvironmentVariable(PATH,Machine)1.2.5 条件筛选环境变量Get-ChildItemEnv:|Where-Object{$_.Name-matchPATH}2 环境变量修改操作2.1 临时修改 PATH会话级2.1.1 CMD 环境set PATH%PATH%;C:\自定义路径生效范围当前 CMD 窗口窗口关闭后配置自动恢复。2.1.2 PowerShell 环境$env:PATH;C:\自定义路径生效范围当前 PowerShell 窗口窗口关闭后配置自动恢复。2.2 永久修改 PATH持久化2.2.1 CMD 环境setx 方式当前用户级别无需管理员权限setx PATH %PATH%;C:\自定义路径系统全局级别需管理员权限setx PATH %PATH%;C:\自定义路径 /m限制说明setx对字符串长度限制为 1024 字符超出部分将被截断。2.2.2 PowerShell 环境无长度限制推荐当前用户级别$userPath[Environment]::GetEnvironmentVariable(PATH,User)$newPath$userPath;C:\自定义路径[Environment]::SetEnvironmentVariable(PATH,$newPath,User)系统全局级别需管理员权限$systemPath[Environment]::GetEnvironmentVariable(PATH,Machine)$newPath$systemPath;C:\自定义路径[Environment]::SetEnvironmentVariable(PATH,$newPath,Machine)2.2.3 CMD 注册表操作无长度限制当前用户级别reg add HKCU\Environment /v PATH /t REG_EXPAND_SZ /d %PATH%;C:\自定义路径 /f系统全局级别需管理员权限reg add HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment /v PATH /t REG_EXPAND_SZ /d %PATH%;C:\自定义路径 /f3 永久修改后即时生效方法重启终端窗口关闭现有终端重新启动即可加载最新环境变量。Chocolatey 刷新命令refreshenv重启 Windows 资源管理器任务管理器 → 找到Explorer.exe→ 右键选择重启。当前终端临时同步CMDset PATH%PATH%PowerShell$env:Path [Environment]::GetEnvironmentVariable(Path,User);[Environment]::GetEnvironmentVariable(Path,Machine)4 环境变量删除操作4.1 临时删除当前会话4.1.1 CMD 环境set 变量名4.1.2 PowerShell 环境Remove-ItemEnv:变量名$env:变量名 $null4.2 永久删除4.2.1 CMD 环境用户级别setx 变量名 系统级别需管理员权限setx 变量名 /m注册表删除reg delete HKCU\Environment /v 变量名 /f reg delete HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment /v 变量名 /f4.2.2 PowerShell 环境用户级别[Environment]::SetEnvironmentVariable(变量名,$null,User)系统级别需管理员权限[Environment]::SetEnvironmentVariable(变量名,$null,Machine)4.2.3 从 PATH 中移除指定路径用户级别$path[Environment]::GetEnvironmentVariable(PATH,User)$newPath($path-split;|Where-Object{$_-notlikeC:\待移除路径})-join;[Environment]::SetEnvironmentVariable(PATH,$newPath,User)系统级别需管理员权限$path[Environment]::GetEnvironmentVariable(PATH,Machine)$newPath($path-split;|Where-Object{$_-notlikeC:\待移除路径})-join;[Environment]::SetEnvironmentVariable(PATH,$newPath,Machine)5 操作规范与风险规避追加路径时必须保留原有 PATH 取值避免覆盖系统路径。路径包含空格时需使用英文双引号包裹完整路径字符串。多条路径之间使用英文分号;分隔。避免使用setx修改长度超过 1024 字符的 PATH推荐采用 PowerShell 或注册表方式。执行修改操作前建议备份当前 PATH 取值echo %PATH% path_backup.txt永久修改完成后需重启终端验证配置状态。6 多终端同步机制6.1 同步原理系统环境变量为全局配置源各类终端仅在启动时读取环境变量运行期间不自动同步。6.2 同步方法统一使用 PowerShell 或注册表方式修改全局环境变量。修改完成后关闭全部终端重新启动以加载最新配置。安装 Chocolatey 后可使用refreshenv命令实现全局即时刷新。6.3 冲突规避规则不在多个终端内同时执行临时 PATH 修改操作。不使用setx处理长路径配置。长期开启的终端需定期重启保持配置同步。7 命令速查表7.1 CMD 常用命令查看echo %PATH%、path、set临时修改set PATH%PATH%;路径永久用户setx PATH %PATH%;路径永久系统setx PATH %PATH%;路径 /m生效重开终端 或refreshenv删除setx 变量名 7.2 PowerShell 常用命令查看$env:PATH、Get-ChildItem Env:临时修改$env:PATH ;路径永久用户$p[Environment]::GetEnvironmentVariable(PATH,User);[Environment]::SetEnvironmentVariable(PATH,$p;路径,User)永久系统管理员$p[Environment]::GetEnvironmentVariable(PATH,Machine);[Environment]::SetEnvironmentVariable(PATH,$p;路径,Machine)删除用户变量[Environment]::SetEnvironmentVariable(变量名,$null,User)删除系统变量[Environment]::SetEnvironmentVariable(变量名,$null,Machine)Referencerefresh environment variables without reboot windows 10 | Search For Funhttps://search4fan.github.io/post/win10_new_system_variable_without_restart.htmlRefresh Environment Variables in Windows 11 (Without Reboot)https://thecategorizer.com/windows/how-to-refresh-environment-variables-in-windows/How do you add a Windows environment variable without rebooting? - 2009https://serverfault.com/questions/8855/how-do-you-add-a-windows-environment-variable-without-rebooting