程序员、系统管理员和高级用户可能需要在某些时候使用环境变量。有些人可能想删除一个环境变量;其他人会想要改变它的价值,等等。本指南分享如何编辑或删除环境变量,以及如何在 Windows 中取消设置环境变量:
打开环境变量(Environment Variables)窗口
要进行本文中显示的许多编辑,您首先需要打开“环境变量(Environment Variables)”窗口。本指南解释了如何做到这一点并向您展示有关使用环境变量的基础知识:Windows 中的环境变量是什么?(What are environment variables in Windows?).
Windows 10中的“环境变量(Environment Variables)”窗口
如果您想跳过阅读它,在所有版本的Windows中工作相同的一个路径是打开Run window (Win + R)、命令提示符(Command Prompt)或PowerShell并执行命令:rundll32.exe sysdm.cpl,EditEnvironmentVariables。
如何在Windows中编辑环境变量(Windows)
如果要更改现有环境变量的值,请首先在“环境变量(Environment Variables)”窗口中选择它。然后,单击或点按编辑(Edit)。
如何在Windows 10中编辑环境变量(Windows 10)
您将看到一个窗口,您可以在其中编辑变量的名称和值。进行所需的修改,然后按OK。然后,在Environment Variables窗口中再按一次OK 。
编辑环境变量
如何从命令提示符编辑环境变量(Command Prompt)
您也可以从命令提示符(Command Prompt)创建新环境变量,或编辑现有环境变量的值(但不是其名称)。您必须输入的命令是:
- setx variable_name “value”如果要创建用户环境变量
- setx variable_name “value” /m如果你要创建一个系统环境变量
例如,我们键入setx TEST “C:\digitalcitizen”并创建一个名为 TEST 的用户变量,其值为C:\digitalcitizen.
如何使用命令提示符设置环境变量(Command Prompt)
如果我们想更改环境变量的值,我们可以运行相同的setx命令,但为该变量指定一个新值。例如,执行setx TEST “C:\DC”TEST环境变量的值更改为C:\DC。
如何在命令提示符中更改环境变量的值(Command Prompt)
之所以有效,是因为setx命令会用您键入的最后一个值重写现有值。因此,如果您对同一个变量多次使用此命令,该变量将保留您输入的最后一个值。
如何使用命令提示符向环境变量添加多个值(Command Prompt)
注意:(NOTE:)您可以通过在命令提示符中运行(Command Prompt)set命令(不是 setx,并且不带任何参数)来获取所有可用环境变量的列表。但是,如果您刚刚创建或编辑了环境变量,则必须关闭并重新打开命令提示符(Command Prompt)才能显示更改。
如何在命令提示符中查看所有环境变量(Command Prompt)
如何从PowerShell编辑环境变量(PowerShell)
您还可以从PowerShell(PowerShell)创建或编辑现有环境变量的值。PowerShell命令是:
- [Environment]::SetEnvironmentVariable("variable_name","variable_value","User")如果要创建用户环境变量
- [Environment]::SetEnvironmentVariable("variable_name","variable_value","Machine")如果要创建系统环境变量
例如,我们键入[Environment]::SetEnvironmentVariable("TEST","digitalcitizen.life","User")以创建名为 TEST 的用户环境变量,其值为digitalcitizen.life。要稍后更改变量的值,我们可以使用不同的值运行相同的命令。就像(Just)命令提示符(Command Prompt)中的setx一样,此命令每次运行时都会重写指定变量的值。
如何使用PowerShell设置环境变量(PowerShell)
如果要为一个变量分配多个值,请在命令中键入所有值,每个值之间使用分号,如下图所示。
如何在PowerShell中向环境变量添加多个值(PowerShell)
注意:(NOTE:)在PowerShell中,您可以通过运行Get-ChildItem Env:命令获取所有环境变量的列表。但是,如果您刚刚创建或编辑了环境变量,则必须关闭并重新打开PowerShell才能显示更改。
如何在PowerShell中查看所有环境变量(PowerShell)
如何在Windows中清除环境变量的值(来自命令提示符(Command Prompt))
如果要删除环境变量的值(同时保留其名称),则无法使用“环境变量(Environment Variables)”窗口中的鼠标和键盘来执行此操作。如果您选择一个变量并按Edit,您可以删除该值,但您不能按OK,因为此按钮会变灰。因此,您无法保存更改。
如何在Windows 10中清除环境变量(Windows 10)
但是,您可以使用命令提示符(Command Prompt)清除环境变量的值。要从命令提示符(Command Prompt)取消设置环境变量,请键入命令setx variable_name “”。例如,我们输入了 setx TEST “”(setx TEST “”),这个环境变量现在有一个空值。
如何使用命令提示符清除环境变量(Command Prompt)
接下来,让我们看看如何删除环境变量。
如何在Windows中删除环境变量(Windows)
如果您不想再使用特定环境变量,请在“环境变量(Environment Variables)”窗口中选择它。然后,按Delete。Windows不会要求对此操作进行任何确认。因此,如果您改变了主意,则必须按Cancel,以便不应用删除。如果要继续删除,请按OK。
如何在Windows 10中删除环境变量(Windows 10)
如何从命令提示符中删除环境变量(Command Prompt)
要从Command Prompt中删除环境变量,请键入以下两个命令之一,具体取决于该变量的类型:
- REG delete “HKCU\Environment” /F /V “variable_name” 如果它是用户环境变量,或者
- REG delete “HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment” /F /V “variable_name”如果它是系统环境变量。
例如,我们输入REG delete “HKCU\Environment” /F /V “TEST” 并且我们的TEST环境变量从用户的配置文件中消失了。
如何使用命令提示符在(Command Prompt)Windows中取消设置环境变量(Windows)
如何从PowerShell中删除环境变量(PowerShell)
要从PowerShell取消设置和删除环境变量,请键入命令:
- [Environment]::SetEnvironmentVariable("variable_name", $null ,"User")如果它是用户配置文件变量,或者
- [Environment]::SetEnvironmentVariable("variable_name", $null ,"Machine")如果它是系统范围的变量。
例如,我们输入[Environment]::SetEnvironmentVariable("TEST", $null ,"User") 并且这个环境变量从用户的配置文件中消失了。
如何从PowerShell中删除环境变量(PowerShell)
就是这样!
为什么要学习如何在Windows中编辑和删除环境变量?
你现在知道如何做这一切了。但是为什么要更改或编辑环境变量?是因为您的系统中存在您不再使用的某些应用程序的剩余变量吗?或者是因为你有一个特殊的设置,你需要使用环境变量?在下面的评论部分让我们知道。
How to edit, clear, and delete environment variables in Windows -
Programmers, system administrators, and power users may need to work at some point with enνironment variables. Some may want to delete an enνironmеnt variable; others will want to change its value, and ѕo on. This guide shares how to edit or delete environment variables, as well as how to unѕet environmеnt variables in Windowѕ:
Open the Environment Variables window
To make many of the edits shown in this article, you first need to open the Environment Variables window. This guide explains how to do that and shows you the basics about working with environment variables: What are environment variables in Windows?.
The Environment Variables window in Windows 10
If you want to skip reading it, one path that works the same in all versions of Windows is to open the Run window (Win + R), Command Prompt, or PowerShell and execute the command: rundll32.exe sysdm.cpl,EditEnvironmentVariables.
How to edit an environment variable in Windows
If you want to change the value of an existing environment variable, first select it in the Environment Variables window. Then, click or tap Edit.
How to edit an environment variable in Windows 10
You are shown a window where you can edit both the name and the value of the variable. Make the modifications you desire and press OK. Then, press OK one more time in the Environment Variables window.
Editing an environment variable
How to edit an environment variable from Command Prompt
You can create a new environment variable, or edit the value of an existing environment variable (but not its name) from the Command Prompt too. The command you have to enter is:
- setx variable_name “value” if you want to create a user environment variable
- setx variable_name “value” /m if you’re going to create a system environment variable
For example, we typed setx TEST “C:\digitalcitizen” and created a user variable named TEST with the value C:\digitalcitizen.
How to set an environment variable using Command Prompt
If we want to change the value of an environment variable, we can run the same setx command but specify a new value for the variable. For instance, executing setx TEST “C:\DC” changes the value of the TEST environment variable to C:\DC.
How to change the value of an environment variable in Command Prompt
That works because the setx command rewrites the existing value with the last one you type. Therefore, if you use this command multiple times on the same variable, the variable will keep the last value that you typed.
How to add multiple values to an environment variable using Command Prompt
NOTE: You can get a list of all the environment variables available by running the set command in Command Prompt (not setx, and without any parameters). However, if you just created or edited an environment variable, you must close and reopen Command Prompt for the changes to show up.
How to see all the environment variables in Command Prompt
How to edit an environment variable from PowerShell
You can also create or edit the value of an existing environment variable from PowerShell. The PowerShell command for that is:
- [Environment]::SetEnvironmentVariable("variable_name","variable_value","User") if you want to create a user environment variable
- [Environment]::SetEnvironmentVariable("variable_name","variable_value","Machine") if you want to create a system environment variable
For instance, we typed [Environment]::SetEnvironmentVariable("TEST","digitalcitizen.life","User") in order to create a user environment variable called TEST with the value digitalcitizen.life. To change the value of the variable later, we can run the same command using a different value. Just like setx in Command Prompt, this command rewrites the value of the specified variable each time you run it.
How to set an environment variable with PowerShell
If you want to assign multiple values to a variable, type all of them in the command, with semicolons between each value, as illustrated below.
How to add multiple values to an environment variable in PowerShell
NOTE: In PowerShell, you can get a list of all the environment variables by running the Get-ChildItem Env: command. However, if you just created or edited an environment variable, you must close and reopen PowerShell for the changes to show up.
How to see all the environment variables in PowerShell
How to clear the value of an environment variable in Windows (from Command Prompt)
If you want to remove the value of an environment variable (while keeping its name), you cannot do that with the mouse and keyboard from the Environment Variables window. If you select a variable and press Edit, you can delete the value, but you cannot press OK, as this button gets grayed out. Therefore you cannot save your changes.
How to clear an environment variable in Windows 10
However, you can clear the value of an environment variable using Command Prompt. To unset an environment variable from Command Prompt, type the command setx variable_name “”. For example, we typed setx TEST “” and this environment variable now had an empty value.
How to clear an environment variable with Command Prompt
Next, let’s see how to remove an environment variable.
How to delete an environment variable in Windows
If you no longer want to use a particular environment variable, select it in the Environment Variables window. Then, press Delete. Windows does not ask for any confirmation of this action. Therefore, if you have changed your mind, you must press Cancel, so that the removal is not applied. If you want the deletion to go ahead, press OK.
How to delete an environment variable in Windows 10
How to delete an environment variable from Command Prompt
To delete an environment variable from Command Prompt, type one of these two commands, depending on what type that variable is:
- REG delete “HKCU\Environment” /F /V “variable_name” if it’s a user environment variable, or
- REG delete “HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment” /F /V “variable_name” if it’s a system environment variable.
For example, we typed REG delete “HKCU\Environment” /F /V “TEST” and our TEST environment variable was gone from the user’s profile.
How to unset an environment variable in Windows using Command Prompt
How to delete an environment variable from PowerShell
To unset and delete an environment variable from PowerShell, type the command:
- [Environment]::SetEnvironmentVariable("variable_name", $null ,"User") if it’s a user profile variable, or
- [Environment]::SetEnvironmentVariable("variable_name", $null ,"Machine") if it’s a system-wide variable.
For example, we typed [Environment]::SetEnvironmentVariable("TEST", $null ,"User") and this environment variable was gone from the user’s profile.
How to delete an environment variable from PowerShell
That’s it!
Why did you want to learn how to edit and delete environment variables in Windows?
You now know how to do all that. But why did you want to change or edit environment variables? Was it because there were leftover variables on your system from certain apps that you no longer used? Or was it because you have a special setup, and you need to work with environment variables? Let us know in the comments section below.