You are watching: env-Variable in Windows setzen
Environment variables are not often seen directly when using Windows. However there are cases, especially when using the command line, that setting and updating environment variables is a necessity. In this series we talk about the various approaches we can take to set them. In this article we look at how to interface with environment variables using the Command Prompt and Windows PowerShell. We also note where in the registry the environment variables are set, if you needed to access them in such a fashion.
Print environment variables
You can use environment variables in the values of other environment variables. It is then helpful to be able to see what environment variables are set already. This is how you do it:
Command Prompt
List all environment variables
Command Prompt – C:\>
1set
Output
1 2 3 4 5 6ALLUSERSPROFILE=C:\ProgramData APPDATA=C:\Users\user\AppData\Roaming . . . windir=C:\Windows
Print a particular environment variable:
Command Prompt – C:\>
1echo %ProgramFiles%
Output
1C:\Program Files
Windows PowerShell
List all environment variables
Windows PowerShell – PS C:\>
1Get-ChildItem
Env:
Output
1 2 3 4 5 6 7 8Name Value ---- ----- ALLUSERSPROFILE C:\ProgramData APPDATA C:\Users\user\AppData\Roaming . . . windir C:\Windows
Print a particular environment variable:
Windows PowerShell – PS C:\>
1echo
$Env
:ProgramFiles
Output
1C:\Program Files
Set Environment Variables
To set persistent environment variables at the command line, we will use
setx.exe
. It became part of Windows as of Vista/Windows Server 2008. Prior to that, it was part of the Windows Resource Kit. If you need the Windows Resource Kit, see Resources at the bottom of the page.
setx.exe
does not set the environment variable in the current command prompt, but it will be available in subsequent command prompts.User Variables
Command Prompt – C:\>
1setx EC2_CERT "%USERPROFILE%\aws\cert.pem"
Open a new command prompt.
Command Prompt – C:\>
1echo %EC2_CERT%
Output
1C:\Users\user\aws\cert.pem
System Variables
To edit the system variables, you’ll need an administrative command prompt. See HowTo: Open an Administrator Command Prompt in Windows to see how.
Command Prompt – C:\>
1setx EC2_HOME "%APPDATA%\aws\ec2-api-tools" /M
Registry
Warning This method is recommended for experienced users only.
The location of the user variables in the registry is:
HKEY_CURRENT_USER\Environment
. The location of the system variables in the registry is:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
.When setting environment variables through the registry, they will not recognized immediately. One option is to log out and back in again. However, we can avoid logging out if we send a WM_SETTINGCHANGE message, which is just another line when doing this programatically, however if doing this on the command line it is not as straightforward.
One way is to get this message issued is to open the environment variables in the GUI, like we do in HowTo: Set an Environment Variable in Windows – GUI; we do not need to change anything, just open the
Environment Variables
window where we can see the environment variables, then hitOK
.Another way to get the message issued is to use
setx
, this allows everything to be done on the command line, however requires setting at least one environment variable withsetx
.Printing Environment Variables
With Windows XP, the
reg
tool allows for accessing the registry from the command line. We can use this to look at the environment variables. This will work the same way in the command prompt or in powershell. This technique will also show the unexpanded environment variables, unlike the approaches shown for the command prompt and for powershell.First we’ll show the user variables:
Command Prompt – C:\>
1reg query HKEY_CURRENT_USER\Environment
Output
1 2 3HKEY_CURRENT_USER\Environment TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp TMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
We can show a specific environment variable by adding
/v
then the name, in this case we’ll doTEMP
:
Command Prompt – C:\>
1reg query HKEY_CURRENT_USER\Environment /v TEMP
Output
1 2HKEY_CURRENT_USER\Environment TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
Now we’ll list the system environment variables:
Command Prompt – C:\>
1reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment ComSpec REG_EXPAND_SZ %SystemRoot%\system32\cmd.exe FP_NO_HOST_CHECK REG_SZ NO NUMBER_OF_PROCESSORS REG_SZ 8 OS REG_SZ Windows_NT Path REG_EXPAND_SZ C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\ PATHEXT REG_SZ .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC PROCESSOR_ARCHITECTURE REG_SZ AMD64 PROCESSOR_IDENTIFIER REG_SZ Intel64 Family 6 Model 60 Stepping 3, GenuineIntel PROCESSOR_LEVEL REG_SZ 6 PROCESSOR_REVISION REG_SZ 3c03 PSModulePath REG_EXPAND_SZ %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files\Intel\ TEMP REG_EXPAND_SZ %SystemRoot%\TEMP TMP REG_EXPAND_SZ %SystemRoot%\TEMP USERNAME REG_SZ SYSTEM windir REG_EXPAND_SZ %SystemRoot%
And same as with the user variables we can query a specific variable.
Command Prompt – C:\>
1reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH
Output
1 2HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment PATH REG_EXPAND_SZ C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
Unsetting a Variable
When setting environment variables on the command line,
setx
should be used because then the environment variables will be propagated appropriately. However one notable thingsetx
doesn’t do is unset environment variables. Thereg
tool can take care of that, however anothersetx
command should be run afterwards to propagate the environment variables.The layout for deleting a user variable is:
reg delete HKEY_CURRENT_USER\Environment /v variable_name /f
. If/f
had been left off, we would have been prompted:Delete the registry value EXAMPLE (Yes/No)?
. For this example we’ll delete the user variableUSER_EXAMPLE
:
Command Prompt – C:\>
1reg delete HKEY_CURRENT_USER\Environment /v USER_EXAMPLE /f
Output
1The operation completed successfully.
Deleting a system variable requires administrator privileges. See HowTo: Open an Administrator Command Prompt in Windows to see how to do this.
The layout for deleting a system variable is:
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v variable_name /f
. For this example we’ll delete the system variableSYSTEM_EXAMPLE
:
Command Prompt – C:\>
1reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v SYSTEM_EXAMPLE /f
If this was run as a normal user you’ll get:
Output
1ERROR: Access is denied.
But run in an administrator shell will give us:
Output
1The operation completed successfully.
Finally we’ll have to run a
setx
command to propagate the environment variables. If there were other variables to set, we could just do that now. However if we were just interested in unsetting variables, we will need to have one variable left behind. In this case we’ll set a user variable namedthrowaway
with a value oftrash
Command Prompt – C:\>
1setx throwaway trash
Output
1SUCCESS: Specified value was saved.
Resources
Windows Server 2003 Resource Kit Tools will also work with Windows XP and Windows XP SP1; use Windows XP Service Pack 2 Support Tools with Windows XP SP2. Neither download is supported on 64-bit version.
Parts in this series
See more information related to the topic set env variable in windows
What are Environment Variables ? with Examples on Windows & Linux
- Author: JimShapedCoding
- Post date: 2021-09-27
- Ratings: 4 ⭐ ( 3748 Ratings )
- Match search results: In this video, we will learn how Environment Variables could help us to create more dynamic programs, also we will learn about System ENV Variables, and will go into detail about the PATH Environment Variable.
Also we will learn the PATH env variable, how to use it and what it does to our systemEnvVariables Environment Variables
☕ Buy me a coffee: https://www.buymeacoffee.com/jimsc
Connect with me with:
– Instagram: https://www.instagram.com/jimshapedcoding
– E-mail: jimshapedcoding@gmail.com
– Twitter : https://twitter.com/jimshapedcoding
– Discord : https://discord.com/invite/aMgcPD9???? Comment below if you want to see more videos in that style
???? Subscribe to start your Python Developer / DevOps Engineer journey here:
https://www.youtube.com/channel/UCU8d7rcShA7MGuDyYH1aWGg?sub_confirmation=1My Personal website:
http://jimshapedcoding.comTimeline of the video:
00:00 – 04:51 – Environment Variables
04:52 – 07:14 – System Environment Variables on Windows
07:15 – 10:22 – PATH System Environment Variable on Windows
10:23 – 13:21 – PATH System Environment Variable on Linux
Using .env files to set environment variables in Windows
- Author: stackoverflow.com
- Ratings: 4 ⭐ ( 6522 Ratings )
- Match search results:
How to set python environment variable PYTHONPATH on Windows?
- Author: www.tutorialspoint.com
- Ratings: 5 ⭐ ( 5198 Ratings )
- Match search results: How to set python environment variable PYTHONPATH on Windows? – To set the PYTHONPATH on windows to point Python to look in other directories for module and pac …
about Environment Variables – PowerShell
- Author: docs.microsoft.com
- Ratings: 4 ⭐ ( 3079 Ratings )
- Match search results: Describes how to access and manage environment variables in PowerShell.
How to Set Environment Variables in Windows 11/10
- Author: www.aomeitech.com
- Ratings: 4 ⭐ ( 5704 Ratings )
- Match search results: In this article, we will explain how to add, change and delete environment variables properly in Windows 11 or Windows 10.
Set Environment Variable in Windows {How-To}
- Author: phoenixnap.com
- Ratings: 5 ⭐ ( 5500 Ratings )
- Match search results: This tutorial covers different ways to set, view, and unset environment variables in Windows using the GUI, Command Prompt and PowerShell.
How to Set Environment Variables in Windows Server
- Author: itsiti.com
- Ratings: 5 ⭐ ( 3037 Ratings )
- Match search results: Launch the Windows System Properties (use sysdm.cpl from command prompt). Under Advanced, click Environment Variables. Depending on your requirement, you
See more articles in this category: Computer tips