Set persistent environment variable from cmd.exe

I need to set environment variables on different Windows machines, but I don’t want to manually change them by going to the “My Computer” properties screen

I want to do this from the command line using a batch file. As far as I understand, using set will only change the variable for the processes that I will call in the command window.

I want to install it specifically, so later, when starting a new process, it will use the new settings that I set. Is there any way to do this from the command line?

+107
windows cmd environment-variables batch-file
May 05 '11 at 13:03
source share
5 answers

Use the SETX command (note the suffix 'x') to set the variables that remain after the cmd window closes.

Although it is worth reading the “notes” that appear if you type using ( setx /? ), In particular:

2) On the local system, variables created or modified by this tool will be available in future command windows, but not in the current CMD.exe command window.

3) On the remote system, variables created or modified by this tool will be available in the next login session.

In PowerShell, the command [Environment] :: SetEnvironmentVariable .

+165
May 05 '11 at 13:09
source share

The MSDN documentation for environment variables tells you what to do:

To programmatically add or change system variables, add them to the registry key HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Control \ Session Manager \ Environment , and then send the WM_SETTINGCHANGE message with lParam set to the "Environment" line. This allows applications, such as the shell, to receive updates.

To do this, of course, you will need administrator rights. I do not know how to translate a Windows message from a Windows package, so you need to write a small program for this.

+18
May 05 '11 at 1:07 pm
source share
 ' SetVar.vbs Sub sety(wsh, action, typey, vary, value) Dim wu Set wu = wsh.Environment(typey) wui = wu.Item(vary) Select Case action Case "ls" WScript.Echo wui Case "del" On Error Resume Next wu.remove(vary) On Error Goto 0 Case "set" wu.Item(vary) = value Case "add" If wui = "" Then wu.Item(vary) = value ElseIf InStr(UCase(";" & wui & ";"), UCase(";" & value & ";")) = 0 Then wu.Item(vary) = value & ";" & wui End If Case Else WScript.Echo "Bad action" End Select End Sub Dim wsh, args Set wsh = WScript.CreateObject("WScript.Shell") Set args = WScript.Arguments Select Case WScript.Arguments.Length Case 3 value = "" Case 4 value = args(3) Case Else WScript.Echo "Arguments - 0: ls,del,set,add; 1: user,system, 2: variable; 3: value" value = "```" End Select If Not value = "```" Then ' 0: ls,del,set,add; 1: user,system, 2: variable; 3: value sety wsh, args(0), args(1), UCase(args(2)), value End If 
+2
Sep 30 '13 at 2:50
source share

Indeed, SET TEST_VARIABLE = value only works for the current process, so SETX is required. A quick example of persistent storage of an environment variable at the user level.

  1. In cmd, SETX TEST_VARIABLE etc E. Not yet applied ( echo %TEST_VARIABLE% shows %TEST_VARIABLE% ,
  2. Quick check: open cmd, echo %TEST_VARIABLE% shows and etc
  3. Checking the GUI: System Properties → Advanced → Environment Variables → User Variables for → you should see the TEST_VARIABLE variable with the value and etc D.
0
Apr 26 '19 at 13:54 on
source share
 :: Sets environment variables for both the current 'cmd' window :: and/or other applications going forward. :: I call this file keyz.cmd to be able to just type 'keyz' at the prompt :: after changes because the word 'keys' is already taken in Windows. @echo off :: set for the current window set APCA_API_KEY_ID=PK2P5Z3INK0VZT00MRUC set APCA_API_SECRET_KEY=WU2gA4m73LzvBKWv2OkzuHnbXc5sUX0ftCFSBy set APCA_API_BASE_URL=https://paper-api.alpaca.markets :: setx also for other windows and processes going forward setx APCA_API_KEY_ID %APCA_API_KEY_ID% setx APCA_API_SECRET_KEY %APCA_API_SECRET_KEY% setx APCA_API_BASE_URL %APCA_API_BASE_URL% :: Displaying what was just set. set apca :: Or for copy/paste manually ... :: setx APCA_API_KEY_ID 'PKKMQ1QOAVVGV5KKRD26' :: setx APCA_API_SECRET_KEY 'hdOHprwVnDWIml9ICD4hvoCpO7BCy5G3qWB' :: setx APCA_API_BASE_URL 'https://paper-api.alpaca.markets' 
0
Apr 26 '19 at 17:30
source share



All Articles