Windows check if environment variable set

Setting and getting Windows environment variables from the command prompt?

I want to set an environment variable from the command prompt and then be able to access it globally (for instance, I should see it by going to System -> Environment Variables).

When I use the set command, it isn’t accessible in a new cmd session.

6 Answers 6

To make the environment variable accessible globally you need to set it in the registry. As you’ve realised by just using:

you are just setting it in the current process space.

According to this page you can use the setx command:

setx is built into Windows 7, but for older versions may only be available if you install the Windows Resource Kit

We can also use «setx var variable /M» to set the var to system environment variable level instead of user level.

Note: This command should be run as administrator.

/M for set system environment variable level instead of user level like @Minh Chau answer

RESTART command line (if you don’t restart command line, environment variable will not work)

You can use setx env var [/M] as mentioned above. If it doesn’t take effect you can use refreshenv to refresh environment variables. You don’t have to restart your computer, explorer.exe or your command prompt to do that.

Edit: apparantly refreshenv doesn’t come naturally with Windows, so here’s the source: https://pastebin.com/1fJqA0pT
Save as RefreshEnv.cmd and place it in a folder that’s included in your PATH environment variables

System variables can be set through CMD and registry For ex. reg query «HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment» /v PATH

All the commonly used CMD codes and system variables are given here: Set Windows system environment variables using CMD.

Open CMD and type Set

You will get all the values of system variable.

Type set java to know the path details of java installed on your window OS.

Setting a system environment variable from a Windows batch file?

Is it possible to set a environment variable at the system level from a command prompt in Windows 7 (or even XP for that matter). I am running from an elevated command prompt.

When I use the set command ( set name=value ), the environment variable seems to be only valid for the session of the command prompt.

7 Answers 7

The XP Support Tools (which can be installed from your XP CD) come with a program called setx.exe :

I think Windows 7 actually comes with setx as part of a standard install.

Simple example for how to set JAVA_HOME with setx.exe in command line:

Читайте также:  Linux merge text files

This will set environment variable «JAVA_HOME» for current user. If you want to set a variable for all users, you have to use option «-m». Here is an example:

Note: you have to execute this command as Administrator.

Note: Make sure to run the command setx from an command-line Admin window

1 like so: setx /M JAVA_HOME «C:\Progra

1\Java\jdk1.7.0_09″ – samael Jul 31 ’13 at 10:01

If you set a variable via SETX, you cannot use this variable or its changes immediately. You have to restart the processes that want to use it.

Use the following sequence to directly set it in the setting process too (works for me perfectly in scripts that do some init stuff after setting global variables):

For XP, I used a (free/donateware) tool called «RAPIDEE» (Rapid Environment Editor), but SETX is definitely sufficient for Win 7 (I did not know about this before).

System variables can be set through CMD and registry For ex. reg query «HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment» /v PATH

All the commonly used CMD codes and system variables are given here: Set Windows system environment variables using CMD.

Open CMD and type Set

You will get all the values of system variable.

Type set java to know the path details of java installed on your window OS.

Just in case you would need to delete a variable, you could use SETENV from Vincent Fatica available at http://barnyard.syr.edu/

vefatica. Not exactly recent (’98) but still working on Windows 7 x64.

SetX is the command that you’ll need in most of the cases.Though its possible to use REG or REGEDIT

Using registry editing commands you can avoid some of the restrictions of the SetX command — different data types, variables containing = in their name and so on.

REG is the pure registry client but its possible also to import the data with REGEDIT though it allows using only hard coded values (or generation of a temp files). The example here is a hybrid file that contains both batch code and registry data (should be saved as .bat — mind that in batch ; are ignored as delimiters while they are used as comments in .reg files):

Windows Batch Command : How to dereference FOR loop variable to check if that variable is SET in Environment Variable

I’m writing a batch command script where in there is a check for Environment variables. I need to write a FOR loop by passing all the required variables and then validate if it is defined or not and if it is not defined then prompt the value for that key and set that variable permanently.

Problem is that I could not dereference the loop variable and check it in Environment variable.

Sample code is as below:

1″ == []) need to use IF(«!%

1!» == []) – Chandre Gowda Dec 14 ’14 at 23:52

1!» == «») too, but still it goes to ELSE condition – Chandre Gowda Dec 15 ’14 at 0:09

1!»==»» Note no == surrounding spaces, no parentheses. That !%

1! is the variable value, %

1 is variable name – JosefZ Dec 15 ’14 at 0:10

1!==»» ( did not help, it still goes to ELSE condition – Chandre Gowda Dec 15 ’14 at 0:17

Читайте также:  Диск с переменной windows

1!==»» Note double guotes – JosefZ Dec 15 ’14 at 0:20

2 Answers 2

As already indicated, if defined varname is the «correct» way to check if a variable is defined.

To check whether a variable is defined, use the defined keyword in an if statement. help if for more information.

If I may make another suggestion, it might be better to display a folder chooser for the user rather than asking him to key pathnames manually. To that end, save this proof of concept with a .bat extension and try it out. See whether it works like you want:

After you’re satisfied the simulation does what you intend, remove echo from the :chooser subroutine.

See this page for more methods of launching a file or folder chooser from a .bat script, and see this GitHub Gist for more details and more examples of JScript / batch hybrids.

TCL: how to check if environment variable already set

I am trying to construct a module file and I would like to include an if statement to check whether certain environment variables (e.g. PATH, LD_LIBRAY_PATH, PYTHON_PATH, . ) have already been set.

I have tried several different syntax options (after searching on here, official documentation and other forums) but when I then try:

I keep getting ERROR:102: Tcl command execution failed error. Here are a few of the options that I have tried:

What is the correct syntax to check for the existence of environment variables that may have already been set, e.g. in a .cshrc or .login file?

Apologies if this is a basic question. I’ve done stuff like this before in the shell (mainly csh), but I am new to TCL and building module files, so any help and advice wold be greatly appreciated!

3 Answers 3

The http://wiki.tcl.tk/1624 about env contains probably the answer to your question.

Following the hints given in the other answers and the wiki link given above I constructed following code which works for me.

the env global array could be also used to set the value of the environment variable, and it is better to use ::env instead of env to access it from the level 0 (or declare it as global env )

It really helps if you have the actual error message and not the summary “something went wrong”.

Generally speaking, environment variables are mapped into elements of the global env array in Tcl; they don’t just become individual global variables (as that would be open to too much abuse). Also, when talking about a variable you don’t read from it and so don’t prefix it with $ (which in Tcl is strictly a read operation). That said, using the setenv is important because that is about manipulating the environment in the caller, not in the Tcl code itself.

So we use a variation on your second attempt.

The problem with the first attempt is that you need to use square-bracket syntax to use the output of a Tcl command. The problem with the third one is that $? doesn’t mean anything to Tcl at all (I guess we could change that, but it would be a long road to walk and it doesn’t help you now).

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.

Читайте также:  Проверка драйвера видеокарты linux mint

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.

Оцените статью