- HowTo: Set an Environment Variable in Windows — Command Line and Registry
- Print environment variables
- Command Prompt
- Command Prompt — C:\>
- Output
- Command Prompt — C:\>
- Output
- Windows PowerShell
- Windows PowerShell — PS C:\>
- Output
- Windows PowerShell — PS C:\>
- Output
- Set Environment Variables
- User Variables
- Command Prompt — C:\>
- Command Prompt — C:\>
- Output
- System Variables
- Command Prompt — C:\>
- Registry
- Printing Environment Variables
- How to print environment variables to the console in PowerShell?
- 3 Answers 3
- How can I display the contents of an environment variable from the command prompt in Windows 7?
- 8 Answers 8
- Printing all environment variables in C / C++
- 10 Answers 10
- Not the answer you’re looking for? Browse other questions tagged c++ c or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
HowTo: Set an Environment Variable in Windows — Command Line and Registry
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:\>
Output
Print a particular environment variable:
Command Prompt — C:\>
Output
Windows PowerShell
List all environment variables
Windows PowerShell — PS C:\>
Output
Print a particular environment variable:
Windows PowerShell — PS C:\>
Output
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:\>
Open a new command prompt.
Command Prompt — C:\>
Output
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:\>
Registry
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 hit OK .
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 with setx .
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.
How to print environment variables to the console in PowerShell?
I’m starting to use PowerShell and am trying to figure out how to echo a system environment variable to the console to read it.
Neither of the below are working. The first just prints %PATH% , and the second prints nothing.
3 Answers 3
Prefix the variable name with env :
For example, if you want to print the value of environment value «MINISHIFT_USERNAME», then command will be:
You can also enumerate all variables via the env drive:
In addition to Mathias answer.
Although not mentioned in OP, if you also need to see the Powershell specific/related internal variables, you need to use Get-Variable :
These also include stuff you may have set in your profile startup script.
The following is works best in my opinion:
- It’s shorter and therefore a little bit easier to remember than Get-ChildItem . There’s no hierarchy with environment variables.
- The command is symmetrical to one of the ways that’s used for setting environment variables with Powershell. (EX: Set-Item -Path env:SomeVariable -Value «Some Value» )
- If you get in the habit of doing it this way you’ll remember how to list all Environment variables; simply omit the entry portion. (EX: Get-Item Env: )
I found the syntax odd at first, but things started making more sense after I understood the notion of Providers. Essentially PowerShell let’s you navigate disparate components of the system in a way that’s analogous to a file system.
What’s the point of the trailing colon in Env: ? Try listing all of the «drives» available through Providers like this:
I only see a few results. (Alias, C, Cert, D, Env, Function, HKCU, HKLM, Variable, WSMan). It becomes obvious that Env is simply another «drive» and the colon is a familiar syntax to anyone who’s worked in Windows.
You can navigate the drives and pick out specific values:
How can I display the contents of an environment variable from the command prompt in Windows 7?
In Windows 7, when I start the Command prompt, is there any command to display the contents of an environment variable (such as the JAVA_HOME or PATH variables)?
I have tried with echo $PATH , echo PATH and $PATH but none of these work.
8 Answers 8
In Windows Command-Prompt the syntax is echo %PATH%
To get a list of all environment variables enter the command set
To send those variables to a text file enter the command set > filename.txt
To complement the previous answer, if you’re using Powershell echo %PATH% would not work. You need to use the following command instead: echo $Env:PATH
As an additional bit of information: While SET works with global or system variables, sometimes you want to write and read User variables, and this is done with the SETX command. SETX is included in the base installs of Windows beginning with Vista, but was also available in Windows XP by installing the Resource Pack.
One difference about SETX though is that you cannot read the variable out in the same command window you wrote it in. You have to write the SETX command in one Command or Powershell window, and then open a new window to read it using ECHO.
SETX can also write global or system variables.
To Set a user variable using SETX:
To set a global or system variable using SETX:
To read a user or global variable:
Remember, you must open a new Command or Powershell window to read this variable.
Printing all environment variables in C / C++
How do I get the list of all environment variables in C and/or C++?
I know that getenv can be used to read an environment variable, but how do I list them all?
10 Answers 10
The environment variables are made available to main() as the envp argument — a null terminated array of strings:
I think you should check environ . Use «man environ».
Your compiler may provide non-standard extensions to the main function that provides additional environment variable information. The MS compiler and most flavours of Unix have this version of main:
where the third parameter is the environment variable information — use a debugger to see what the format is — probably a null terminated list of string pointers.
EDIT: only works on windows.
In most environments you can declare your main as:
envp contains all environment strings.
If you’re running on a Windows operating system then you can also call GetEnvironmentStrings() which returns a block of null terminated strings.
Most of the answers here point out the possibility to pick the environment from an argument to main supported by most compilers. While Alex’ answer:
should work always, I wonder what happens to char **environ when you manipulate the environment in your C code (putenv, unsetenv). Then environ may point to somewhere else (when it was reallocated, may depend on the system implementation). If we stick to a parameter passed to main and pass it on to the function requiring it, this pointer may not point to the current environment any more.
Not the answer you’re looking for? Browse other questions tagged c++ c or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.