SETX.exe (Resource Kit, Windows 7)

Set environment variables permanently, SETX can be used to set Environment Variables for the machine (HKLM) or currently logged on user (HKCU):

Syntax

   SETX [/s Computer [Credentials]] Variable Value [/m]

   SETX [/s Computer [Credentials]] [Variable] /k RegistryPath [/m]

   SETX [/s Computer [Credentials]] /f FileName {[Variable] {/a L,T | /r oL,oT "SearchString"}
        [/m] | /x} [/d Delimiters]

Key:

   /s Computer
      The name or IP address of a remote computer.
      Do not use backslashes. (default = the local computer)

   Credentials
      The username and password used to connect to Computer:
         /u [Domain\]UserName [/p [Password]]]

   /u [Domain\]UserName
      Run the script with the credentials of the specified user account.
      The default value is the system permissions.

   /p [Password]
      The password of the user account that is specified in the /u parameter.

   Variable
      The name of the environment variable that you want to set.

   Value
      The value to which you want to set the environment variable.

   /k RegistryPath
      Set the variable based on information from a registry key.
      The registry path consists of: \HIVE\KEY\...\Value
      For example:
      HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName

   /f FileName
      The file that you want to read.

   /a L,T
      Get token T from line L in the file. 
      The L (line) and T (token) coordinates are numbered starting from 0.

   /r oL,oT "SearchString"
      Search the file for a token that matches SearchString, case insensitive.
      The offset oL (line) and oT (token) coordinates are numbered starting from 0.
      Negative numbers for oL & oT will count upwards and left respectively.
      If the token is not found, %errorlevel% will be set to 1.

   /m
      Set the variable in the system environment HKLM.
      (The default is the local environment HKCU)

   /x
      Display the file with coordinates against each token.

   /d Delimiters
      Specifies delimiters such as "," or "\" to be used in addition to the four
      built-in delimiters: SPACE, TAB, CR, and LINEFEED.
      Valid delimiters include any ASCII character.
      The maximum number of delimiters is 15, including built-in delimiters.

Because SETX writes variables to the master environment in the registry, edits will only take effect when a new command window is opened - they do not affect the current CMD or PowerShell session.

Environment variables are stored in the registry:

User Variables: HKEY_CURRENT_USER\Environment
System Variables: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Machine variables are stored on the machine and will not follow a users roaming profile. To set a machine variable (/m) requires Administrator rights.

User variables are stored in the user profile and will follow the users roaming profile.

If variables with the same name are stored as both User and Machine Environment variables, the user variable will take precedence. If a Session variable is created that will take precedence over any User and/or Machine Environment variable with the same name.

To edit environment variables in the Windows GUI: Control Panel | System | Advanced | Environment Variables

To delete an environment variable either use the GUI (recommended) or delete the value from the registry with REG delete HKCU\Environment /V _myvar
Deleting a variable with REG will not take effect until next logon due to caching of registry data.
Setting value of "" (empty quotes) will appear to delete the variable - it's not shown by SET but the variable name will remain in the registry.

 

SETX may be used to set environment variables from one of three sources (modes): String Value Mode, Registry Mode, or File Mode.

String Value Mode

Setting environment variables equal to a simple string is the most basic and common usage of SetX.

Examples: (String Value mode)

Set the variable _car_park to be 'South' in the users environment:

SetX _car_park South

Set the variable _car_park to be 'North' in the MACHINE environment:

SetX _car_park North /m

Store the value of _car_park in a second environment variable
SetX _oldcarpark %_car_park%

Set the _car_park environment variable in the local environment to 'West' on a remote computer named Computer64:

SetX /s computer64 /u ss64dom\user64 /p P7BmqgWZ1a _car_park West

Set the _MyPath environment variable in the local environment to use the search path defined in the %PATH% environment variable:

SetX _MyPath %PATH%

Set the _MyPath environment variable in the local environment to use the search path defined in the %PATH% environment variable after replacing ~ with %:

SetX _MyPath ~PATH~

Set the _MyPath environment variable in the local environment to use the search path defined in the PATH environment variable on a remote computer named Computer64:

SetX /s computer64 /u ss64dom\user64 /p pa55w0rd _MyPath %PATH%

Registry Mode

In registry mode, SetX is an alternative to the REG QUERY command.

The only supported hives are: HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE.

REG_DWORD, REG_EXPAND_SZ, REG_SZ, and REG_MULTI_SZ are the valid RegKey data types.
When reading REG_MULTI_SZ values from the registry, only the first item will be extracted and used.
REG_DWORD registry values are extracted and used in hexadecimal mode.

Examples: (Registry mode)

Set the _TZone environment variable in the local environment to the value found in the HKLM...\StandardName registry key:

SetX _TZone /k HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName 

Set the _Build environment variable in the system environment to the value found in the HKLM\...\CurrentBuildNumber registry key:

SetX _Build /k "HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\CurrentBuildNumber" /m

File Mode

File mode supports the parsing of plain text files only, (with CR/LF line endings).

Examples: (File mode)

Display the tokens in a file:

C:> SetX /f filename.txt /x
(0,0 demo1)(0,1 demo2)(0,2 demo3)
(1,0 demoA)(1,1 demoB)(1,2 demoC)

Set the _ipaddr variable in the local environment to the value found at the coordinate 5,11 in the file Ipconfig.out, the variable will only be visible in a new command shell:

SetX _ipaddr /f ipconfig.out /a 5,11

When using SetX to extract values from a file, we can ignore the variable that is set and instead use FOR /F to grab the extracted token text into the current session. In this way we are using the SetX command more like Findstr.
So to obtain the fourth token in the 1st line of example.txt:

For /F "tokens=3" %%G in ('setx /F example.txt dummyVar /A 1^,4 ^|find "Extracted value"') do set _result=%%G
:: remove the trailing period
set _result=%result:~0,1%
Echo %_result%

Thanks to Aacini for suggesting improvements to this page over in the forum.

Early versions of SETX (XP) do not support the options /k,/u,/p or /s and have options prefixed with - instead of /.

"You are never dedicated to something you have complete confidence in. No-one is fanatically shouting that the sun is going to rise tomorrow. When people are fanatically devoted to political or religious faiths or any other kind of dogmas or goals, its always because these dogmas or goals are in doubt" ~ Robert M Pirsig

Related:

Syntax - Environment Variables - List of default variables
SET - Display, set, or remove environment variables for a session
REG - Delete keys or values from the registry
Q104011 - Modify variables by editing the Registry
StackOverflow - Storing a Newline in a variable
SETENV - Vincent Fatica's improved version
Powershell: Set-Variable - Set a variable and a value (set/sv)
Equivalent bash command (Linux): setenv - Display, set, or remove environment variables.


© Copyright SS64.com 1999-2014
Some rights reserved