Setx - DOS/Command Prompt Reference
[Windows Vista or later] Modifies the environment variable settings for individual users or the entire system.
Syntax
One of the following patterns will be used.
setx[.exe] [/S <computer> [/U [<domain>\]<user> [/P [<password>]]]] <variable-name> <value> [/M]
setx[.exe] [/S <computer> [/U [<domain>\]<user> [/P [<password>]]]] <variable-name> /K <reg-path>
setx[.exe] [/S <computer> [/U [<domain>\]<user> [/P [<password>]]]] <variable-name> /F <file-name> {/A <coord> | /R <coord> <search-string>} [/M] [/D <delimiter>]
setx[.exe] [/S <computer> [/U [<domain>\]<user> [/P [<password>]]]] /F <file-name> /X [/D <delimiter>]
Options
/S <computer>, /U [<domain>\]<user>, /P [<password>] |
Specifies the remote computer and username (and password). ‘/S’ is followed by the computer name, ‘/U’ is followed by the username (and domain if necessary), and ‘/P’ is followed by the password (if only ‘/P’ is used, a prompt for password input will be displayed). If ‘/U’ is omitted, it attempts authentication with the current logged-in user. * Specifying the local computer name for <computer> is not allowed. To change settings for a different local user, execute it in combination with Runas. |
/M | Reflects the variables to be written as system-wide settings. Writing to the registry, which holds the system-wide environment variable settings, requires write permissions (typically administrative privileges). |
<variable-name> | Specifies the name of the environment variable to be written as a setting. It is required unless the ‘/X’ option is used. The order of each switch is arbitrary, so the first string without a preceding switch is treated as the variable name. |
<value> | Specifies the value of the environment variable to be written as a setting. The leading and trailing double quotation marks ‘"’ are removed, so if double quotation marks are needed as part of the value, they should be specified as ‘\"’. |
/K <reg-path> |
Retrieves the value from the registry location ‘<reg-path>’ and set it as the value of the environment variable. A registry path must begin with either ‘HKEY_LOCAL_MACHINE (or HKLM)’ or ‘HKEY_CURRENT_USER (or HKCU)’. The types of registry values must be ‘REG_SZ’, ‘REG_EXPAND_SZ’, ‘REG_MULTI_SZ’, and ‘REG_DWORD’ (‘REG_QWORD’ cannot be used). * If <reg-path> is a key name, the ‘Default Value’ for that key (commonly denoted as ‘(Default)’ in the registry editor) will be used. |
/F <file-name> | Attempts to extract values from the specified file. The content of the file is used as text, and the text is divided by delimiter characters and line breaks (CR+LF or LF). For each separated string, coordinates corresponding to the position are provided, with the Y-coordinate increasing for portions separated by line breaks and the X-coordinate increasing for portions separated by other delimiter characters. These coordinates are used with the /A switch or /R switch to obtain the strings to be used as the actual values of environment variables. |
/A <coord> | /R <coord> <search-string> |
(Either /A or /R can be used.) The /A switch corresponds to absolute coordinates in the file with the origin at the beginning (0,0) and retrieves the text in the file at those coordinates, setting that text as the value of the environment variable. The coordinates for <coord> must be the form ‘p,q’, where ‘p’ represents the Y-coordinate (line number starting from 0), and ‘q’ represents the X-coordinate (horizontal index value) as numerical values. This specification is comma-separated and cannot include spaces. (Example: ‘2,3’ : indicates the text at the third digit (4th block in the line) of the second line (3rd line from the beginning).) The /R switch searches for text (blocks) in the file that matches the text specified by <search-string>, and retrieves the text in the file at coordinates corresponding to the relative coordinates with the position set as ‘0,0’ (the format of <coord> is the same as /A). For example, if ‘Something’ is found at the position ‘6,1’ first during the search from the beginning, ‘/R 2,2 Something’ will retrieve the text at the position ‘8,3’ (which is effectively the same as specifying ‘/A 8,3’). Note that case sensitivity is not considered. |
/D <delimiter> |
Specifies additional delimiter characters for (horizontal) text separation. By default, space characters, tab characters, and carriage return (CR) are used as horizontal delimiters, and line feed (LF) is used as the vertical delimiter. Delimiter characters, including the default characters excluding LF, can be used up to a maximum of 15 characters (so you can specify up to 12 characters for <delimiter>). Note that for horizontal delimiters, regardless of type, consecutive delimiter characters are treated as a single delimiter, and leading and trailing delimiter characters are ignored. * You cannot specify additional vertical delimiter characters. |
/X |
Outputs the results of dividing the file specified by /F (including those specified by /D) by delimiter characters. The output is formatted on a per-line basis, such as ‘(p,q text)’, providing hints when using /A or /R. This option does not perform the writing of environment variable settings. |
Details
Setx is used to write pairs of environment variable names and values to the registry, allowing the continued use of those contents thereafter. It differs from the Set command in the following aspects.
- Setx persists the settings, whereas Set is limited to the environment in which it is used
- Changes made by Setx have no effect on the current environment
Therefore, Setx is mainly suitable for purposes such as setup programs and installation processes.
By default, it is stored as an environment variable for the current user. However, using the /M switch will save it as a system-wide setting.
* Executing ‘rundll32 sysdm.cpl,EditEnvironmentVariables’ allows you to change the settings of these environment variables through a GUI. In this UI, ‘System Environment Variables’ corresponds to the destination of settings when using the /M switch, while ‘User Environment Variables’ corresponds to the destination of settings when not using /M.
* The destination for user-specific environment variable settings is ‘HKEY_CURRENT_USER\Environment
’, while the destination for system-wide environment variable settings is ‘HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
’. Additionally, if the setting value contains the ‘%’ character, it is saved as a ‘REG_EXPAND_SZ’ value; otherwise, it is saved as a ‘REG_SZ’ value.
Reflection of setting environment variable
The settings for environment variables made with Setx are written to the registry and notified on the desktop. Therefore, changes will not be reflected for already running applications, including the command prompt (batch file) in which Setx was executed. Even after running Setx, changes may not be reflected if a new application is launched with an already running application as its parent. If changes are not reflected, it may be necessary to close all folder windows, reset Explorer, or restart the computer.
File content and coordinates with the /F switch
When using the /F switch, it reads the value to set in the environment variable from a file. However, in this case, you need to specify "coordinates" with the /A or /R switch. These coordinates are determined by text splitting using a delimiter. For example, if the /D switch is not specified,
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
... text files with above content are divided as follows (output result with the /X switch):
(0,0 Lorem)(0,1 ipsum)(0,2 dolor)(0,3 sit)(0,4 amet,) (1,0 consectetur)(1,1 adipiscing)(1,2 elit,) (2,0 sed)(2,1 do)(2,2 eiusmod)(2,3 tempor)(2,4 incididunt)(2,5 ut)(2,6 labore)(2,7 et)(2,8 dolore)(2,9 magna)(2,10 aliqua.)
Specifying ‘/A 0,4’ for this file uses ‘amet’, and specifying ‘/R 2,1 sit’ uses ‘incididunt’ as the value for the environment variable.
Samples
Sample 1
setx MYHOME D:\MyHome
Creates and saves an environment variable named ‘MYHOME’ with the value ‘D:\MyHome’ (overwrites if ‘MYHOME’ already exists).
Sample 2
set DRINK=Beer setx DRINK "%DRINK%"
Sets the value of the environment variable ‘DRINK’ to ‘Beer’ and applies it to both the current environment and user settings. Since Setx does not reflect settings in the current environment, it is set with the Set command first, and then the value is passed to Setx.
Sample 3
setx PATHEXT "%PATHEXT%"
Saves the current value of the environment variable PATHEXT as the default setting for the logged-in user.
Sample 4
setx APP_ROOT /K "HKLM\Software\Lily System\Launcher\Path" /M
Sets the value of the environment variable named ‘APP_ROOT’ with the value stored in the registry key ‘HKEY_LOCAL_MACHINE\Software\Lily System\Launcher\Path’ and creates/saves this name-value pair as a system-wide environment variable.
Sample 5
setx HOST_DEVICE_NAME /F deviceInfo.txt /R 0,1 Name /D :
Splits the content of the file ‘deviceInfo.txt’ using the delimiter ‘:’, picks the text block that appears immediately after the ‘Name’ text block, and sets(saves) its value as the environment variable ‘HOST_DEVICE_NAME’. For example, when ‘deviceInfo.txt’ contains...
Address: 192.168.150.1 MAC: 00-11-22-33-44-55 Name: StrangeConsole Details: Unknown
... the value of ‘HOST_DEVICE_NAME’ will be ‘StrangeConsole’.