Setting a system environment variable from a Windows batch file? - windows

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.

The XP Support Tools (which can be installed from your XP CD) come with a program called setx.exe:
C:\Program Files\Support Tools>setx /?
SETX: This program is used to set values in the environment
of the machine or currently logged on user using one of three modes.
1) Command Line Mode: setx variable value [-m]
Optional Switches:
-m Set value in the Machine environment. Default is User.
...
For more information and example use: SETX -i
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:
setx JAVA_HOME "C:\Program Files (x86)\Java\jdk1.7.0_04"
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" (or -m, prior to Windows 7).
Here is an example:
setx /m JAVA_HOME "C:\Program Files (x86)\Java\jdk1.7.0_04"
Note: you have to execute this command as Administrator.
Note: Make sure to run the command setx from an command-line Admin window

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):
SET XYZ=test
SETX XYZ test

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.

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.
#echo off
:: requires admin elevated permissions
::setting system variable
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MyVar /D MyVal
::expandable variable
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /T REG_EXPAND_SZ /v MyVar /D MyVal
:: does not require admin permissions
::setting user variable
REG ADD "HKEY_CURRENT_USER\Environment" /v =C: /D "C:\\test"
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):
REGEDIT4
; #ECHO OFF
; CLS
; REGEDIT.EXE /S "%~f0"
; EXIT
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"SystemVariable"="GlobalValue"
[HKEY_CURRENT_USER\Environment]
"UserVariable"="SomeValue"

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.

Related

SETX not setting system variable when using /M

I am trying to set a persistent system variable. This is for a process in which a batch file refers to the variable, and then edits the variable at the end of the process.
I am using setx with the /M parameter, however, it continues to create the environment variable under the local user instead of the system.
The command I am specifically running is:
setx FN 101 /M
What am I missing?
I did not launch an elevated CMD. Turns out in order for the /M parameter to work, CMD must be run as an administrator.

Batch scripting - adding a new path via registry command (reg add) does not add it to path

I am currently working on a batch script and would like to add new paths to the system environment variables via registry command. Here is my current code:
set newPath=%path%;%NODE_MODULES_DIR%;%NODE_MODULES_DIR%\electron\dist
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Sessions Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%newPath%" /f
call logoff
I do a reg add with new paths. And as I understand, since changes will not take effect until after a logout/restart, I added call logoff to force user to log out.
Problem is when I log back in, I still can't see my new paths under system environment variables.
I do not want to use the setx command because of the 1024 limitation.
Can someone help? Thanks!!
Found what's wrong already.
Turns out that the HKEY_LOCAL_MACHINE path is incorrect.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Sessions Manager\Environment
should be
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
(Session Manager instead of Sessions Manager)
And should've used REG_SZ instead of REG_EXPAND_SZ

How to Open Desktop Properties from CMD?

First post here..
I'm writing a program and I need to change the location of the Desktop from Java, (or CMD).
Is there a CMD command that will open the Desktop properties? (where you can modify the location of the desktop)
Thanks,
You can change the location in the registry.
The following batch file example should set the location to %USERPROFILE%\Desktop, (which is the default).
#Echo Off
Reg Add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop" /V "Path" /T REG_EXPAND_SZ /D "%%USERPROFILE%%\Desktop" /F >Nul
I'll leave it to you to change it to a single cmd.exe command if you'd prefer to do it at the Command prompt or if you think changing the registry another way would be easier for you.
You can enter any valid UNC name or mapped drive in the value of this entry. Just remember that as this is an EXPAND string, you should double up the percent characters if using variables you want to expand when accessed.

Command line to remove an environment variable from the OS level configuration

Windows has the setx command:
Description:
Creates or modifies environment variables in the user or system
environment.
So you can set a variable like this:
setx FOOBAR 1
And you can clear the value like this:
setx FOOBAR ""
However, the variable does not get removed. It stays in the registry:
So how would you actually remove the variable?
To remove the variable from the current environment (not permanently):
set FOOBAR=
To permanently remove the variable from the user environment (which is the default place setx puts it):
REG delete HKCU\Environment /F /V FOOBAR
If the variable is set in the system environment (e.g. if you originally set it with setx /M), as an administrator run:
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOOBAR
Note: The REG commands above won't affect any existing processes (and some new processes that are forked from existing processes), so if it's important for the change to take effect immediately, the easiest and surest thing to do is log out and back in or reboot. If this isn't an option or you want to dig deeper, some of the other answers here have some great suggestions that may suit your use case.
To remove the variable from the current command session without removing it permanently, use the regular built-in set command - just put nothing after the equals sign:
set FOOBAR=
To confirm, run set with no arguments and check the current environment. The variable should be missing from the list entirely.
Note: this will only remove the variable from the current environment - it will not persist the change to the registry. When a new command process is started, the variable will be back.
This has been covered quite a bit, but there's a crucial piece of information that's missing. Hopefully, I can help to clear up how this works and give some relief to weary travellers. :-)
Delete From Current Process
Obviously, everyone knows that you just do this to delete an environment variable from your current process:
set FOO=
Persistent Delete
There are two sets of environment variables, system-wide and user.
Delete User Environment Variable:
reg delete "HKCU\Environment" /v FOO /f
Delete System-Wide Environment Variable:
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOO
Apply Value Without Rebooting
Here's the magic information that's missing! You're wondering why after you do this, when you launch a new command window, the environment variable is still there. The reason is because explorer.exe has not updated its environment. When one process launches another, the new process inherits the environment from the process that launched it.
There are two ways to fix this without rebooting. The most brute-force way is to kill your explorer.exe process and start it again. You can do that from Task Manager. I don't recommend this method, however.
The other way is by telling explorer.exe that the environment has changed and that it should reread it. This is done by broadcasting a Windows message (WM_SETTINGCHANGE). This can be accomplished with a simple PowerShell script. You could easily write one to do this, but I found one in Update Window Settings After Scripted Changes:
if (-not ("win32.nativemethods" -as [type])) {
add-type -Namespace Win32 -Name NativeMethods -MemberDefinition #"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"#
}
$HWND_BROADCAST = [intptr]0xffff;
$WM_SETTINGCHANGE = 0x1a;
$result = [uintptr]::zero
[win32.nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE,[uintptr]::Zero, "Environment", 2, 5000, [ref]$result);
Summary
So to delete a user environment variable named "FOO" and have the change reflected in processes you launch afterwards, do the following.
Save the PowerShell script to a file (we'll call it updateenv.ps1).
Do this from the command line: reg delete "HKCU\Environment" /v FOO /f
Run updateenv.ps1.
Close and reopen your command prompt, and you'll see that the environment variable is no longer defined.
Note, you'll probably have to update your PowerShell settings to allow you to run this script, but I'll leave that as a Google-fu exercise for you.
From PowerShell you can use the .NET [System.Environment]::SetEnvironmentVariable() method:
To remove a user environment variable named FOO:
[Environment]::SetEnvironmentVariable('FOO', $null, 'User')
Note that $null is used to better signal the intent to remove the variable, though technically it is effectively the same as passing '' in this case.
To remove a system (machine-level) environment variable named FOO - requires elevation (must be run as administrator):
[Environment]::SetEnvironmentVariable('FOO', $null, 'Machine')
Aside from faster execution, the advantage over the reg.exe-based method is that other applications are notified of the change, via a WM_SETTINGCHANGE message - while few applications actually listen to that message and update their environment in response, the Windows (GUI) shell does, so that applications subsequently launched via File Explorer / from the desktop / the taskbar / the Start Menu do see the updated environment.
Delete Without Rebooting
The OP's question indeed has been answered extensively, including how to avoid rebooting through powershell, vbscript, or you name it.
However, if you need to stick to cmd commands only and don't have the luxury of being able to call powershell or vbscript, you could use the following approach:
rem remove from current cmd instance
SET FOOBAR=
rem remove from the registry if it's a user variable
REG delete HKCU\Environment /F /V FOOBAR
rem remove from the registry if it's a system variable
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOOBAR
rem tell Explorer.exe to reload the environment from the registry
SETX DUMMY ""
rem remove the dummy
REG delete HKCU\Environment /F /V DUMMY
So the magic here is that by using "setx" to assign something to a variable you don't need (in my example DUMMY), you force Explorer.exe to reread the variables from the registry, without needing powershell. You then clean up that dummy, and even though that one will stay in Explorer's environment for a little while longer, it will probably not harm anyone.
Or if after deleting variables you need to set new ones, then you don't even need any dummy. Just using SETX to set the new variables will automatically clear the ones you just removed from any new cmd tasks that might get started.
Background information: I just used this approach successfully to replace a set of user variables by system variables of the same name on all of the computers at my job, by modifying an existing cmd script. There are too many computers to do it manually, nor was it practical to copy extra powershell or vbscripts to all of them. The reason I urgently needed to replace user with system variables was that user variables get synchronized in roaming profiles (didn't think about that), so multiple machines using the same windows login but needing different values, got mixed up.
I agree with CupawnTae.
SET is not useful for changes to the master environment.
FYI: System variables are in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment (a good deal longer than user vars).
The full command for a system var named FOOBAR therefore is:
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOOBAR
(Note the quotes required to handle the space.)
It is too bad the setx command doesn't support a delete syntax. :(
PS: Use responsibly - If you kill your path variable, don't blame me!
The command in DougWare's answer did not work, but this did:
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v FOOBAR /f
The shortcut HKLM can be used for HKEY_LOCAL_MACHINE.
setx FOOBAR ""
just causes the value of FOOBAR to be a null string. (Although, it shows with the set command with the "" so maybe double-quotes is the string.)
I used:
set FOOBAR=
and then FOOBAR was no longer listed in the set command. (Log-off was not required.)
Windows 7 32 bit, using the command prompt, non-administrator is what I used. (Not cmd or Windows + R, which might be different.)
BTW, I did not see the variable I created anywhere in the registry after I created it. I'm using RegEdit not as administrator.
You can also create a small VBScript script:
Set env = CreateObject("WScript.Shell").Environment("System")
If env(WScript.Arguments(0)) <> vbNullString Then env.Remove WScript.Arguments(0)
Then call it like %windir%\System32\cscript.exe //Nologo "script_name.vbs" FOOBAR.
The disadvantage is you need an extra script, but it does not require a reboot.
By the way I just figured out how to unset a permanent variable that you've set using setx.
Simply write quotation marks, like:
setx myvar ""
And on next cmd window restart, if you search for the variable
set myvar
There won't be anything set for it.
In PowerShell, the way to delete an Registry value is with the Remove-ItemProperty cmdlet. In this case, the persistent value (for future processes) is removed with:
Remove-ItemProperty HKCU:\Environment FOOBAR
And if you wish to remove it from the current environment, you can just remove it from the Env: "drive" like so:
rm env:\FOOBAR
SETX.exe cannot be used to do this.

How to Create Registry key using a batfile

Need to Create a Registry Key using bat file.Can I create Reg Key using Command prompt or a bat file.
The main purpose behind this , I want to create envoirment variable using bat file.
You can use the Windows built-in command line tools, either regedit.exe or reg.exe, see:
Regedit
REG Command in Windows XP
Reading NT's Registry with REG.EXE
Yes u can create Registry Key using Batch file
here is an example:
for disabling task manager using .bat file:
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /f /v DisableTaskMgr /t REG_DWORD /d 1
for enabling task manager:
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableTaskMgr /f
You can take Help by entering reg/? in command prompt for various options.
Enjoy.........
SET variable=string
If you want to create a persistent environment variable (i.e. one that not only applies to the current session) you can use setx. No need to mess around with the registry directly if there is a program to do it for you:
SetX has three ways of working:
Syntax 1:
SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M]
Syntax 2:
SETX [/S system [/U [domain\]user [/P [password]]]] var /K regpath [/M]
Syntax 3:
SETX [/S system [/U [domain\]user [/P [password]]]]
/F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]
Description:
Creates or modifies environment variables in the user or system
environment. Can set variables based on arguments, regkeys or
file input.

Resources