Setx command in Windows

The setx command helps create or modify environment variables in a user or system environment, without requiring programming or script writing.

The setx command helps create or modify environment variables in a user or system environment, without requiring programming or script writing. The Setx command will also take the value of the registry keys and write them into text files.To better understand how to use this command, see the example below.

Setx command syntax

 setx [/ s [/ u [] [/ p []]]] [/ m] 
setx [/ s [/ u [] [/ p []]]] [] / k [/ m]
setx [/ s [/ u [] [/ p []]] / f {[] {/ a, | / r, ""} [/ m] | / x} [/ d]

Setx command parameter

Parameters

Describe

/S

Specify the name or IP address of the remote computer.Do not use backslashes in this parameter.The default value will be the name of the local computer.

/ u []

Run the script with the login information of the specified user account.The default value will be system rights.

/ p []

Specify the password of the user account specified in the parameter / u.

 

Specify the name of the environment variable you want to set.

 

Specify the value for which you want to set the environment variable.

/ k

Specifying that the variable is set based on information from the registry key, the Path parameter will use the following syntax:

 . 

. For example, you can specify the following path:

HKEY_LOCAL_MACHINESystemCurrentControlSet

ControlTimeZoneInformationStandardName

/ f

Specify the file you want to use.

/ a,

Specify absolute coordinates and blank extra spaces as search parameters.

/ r, ""

Specify relative coordinates and empty space (String) as the search parameter.

/ m

Specify variable settings in the system environment.The default setting will be the local environment.

/ x

Display file coordinates, ignore command line options / a, / r and / d.

/ d

Specifying the separators as "," or "" will be used along with four integrated delimiters - SPACE, TAB, ENTER and LINEFEED.Valid delimiters include any ASCII characters.The maximum number of separators is 15, including integrated delimiters.

/?

Show help at thecommand prompt.

Setx command comment

  1. The setx command is similar to the UNIX SETENV utility.
  2. The setx command provides only the command line or unique programming method to directly and permanently set the system's environmental values.System environment variables can be configured manually through the Control Panel or through the registry editor.The set command - an internal component of the command interpreter (Cmd.exe) will only set the user environment variables for the current console window.
  3. You can use the setx command to set values ​​for system and user environment variables from one of three sources (mode): Command line mode, registration mode or file mode (Command Line Mode , Registry Mode, or File Mode).
  4. The setx command will write variables to the main environment into the registry. Variables set with setx are only available in future command windows, not in the current command window.
  5. HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE are the only supported hive.REG_DWORD, REG_EXPAND_SZ, REG_SZ and REG_MULTI_SZ are valid RegKey data types.
  6. When you have access to REG_MULTI_SZ values ​​in the registry, only the first item will be extracted and used.
  7. You cannot use the setx command to delete values ​​that have been added to the local or system environment.You can use a set with a variable name and no value to remove a corresponding value from the local environment.
  8. REG_DWORD registry values ​​will be extracted and used in hexadecimal mode.
  9. File mode only supports parsing of returned text files and feeds (CRLF).

Example setx command

To set an environment variable with the name MACHINE in a local environment to Brand1, enter:

 setx MACHINE Brand1 

To set an environment variable with the name MACHINE in the system environment to Brand1 Computer value, enter:

 setx MACHINE "Brand1 Computer" / m 

To set an environment variable named MYPATH in a local environment using the search path specified in the PATH environment variable, enter:

 setx MYPATH% PATH% 

To set an environment variable named MYPATH in a local environment, use the search path specified in the PATH environment variable after replacing ~ with%, type:

 setx MYPATH ~ PATH ~ 

To place an environment variable with the name MACHINE in a local environment to Brand1 on a remote computer named Computer1, type:

 setx / s computer1 / u maindomhiropln / pp @ ssW23 MACHINE Brand1 

To set an environment variable named MYPATH in a local environment, use the search path specified in the PATH environment variable on the remote computer named Computer1, type:

 setx / s computer1 / u maindomhiropln / pp @ ssW23 MYPATH% PATH% 

To set the environment variable named TZONE in the local environment to the value found in the HKEY_LOCAL_MACHINESystemCurrentControlSetControl registry key

TimeZoneIn informationStandardName, type:

 setx TZONE / k HKEY_LOCAL_MACHINESystemCurrentControlSetControlTimeZoneInformationStandardName 

To set the environment variable named TZONE in the local environment of the remote computer named Computer1 to the value found in the HKEY_LOCAL_MACHINESystemCurrentControlSetControl registry key

TimeZoneInformStandardName, type:

 setx / s computer1 / u maindomhiropln / pp @ ssW23 TZONE / k HKEY_LOCAL_MACHINESystemCurrentControlSetControlTimeZoneInformationStandardName 

To set the BUILD environment variable in the system environment to the value found in the registry key HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsNTCurrentVersion

CurrentBuildNumber, please enter:

 setx BUILD / k "HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsNTCurrentVersionCurrentBuildNumber" / m 

To set the BUILD environment variable in the system environment of the remote computer named Computer1 to the value found in the registry key HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsNT

CurrentVersionCurrentBuildNumber, type:

 setx / s computer1 / u maindomhiropln / pp @ ssW23 BUILD / k "HKEY_LOCAL_MACHINESoftwareMicrosoftWindows NTCurrentVersionCurrentBuildNumber" / m 

To display the contents of the file named Ipconfig.out, along with the corresponding coordinates of the content, enter:

 setx / f ipconfig.out / x 

To set the environment variable named IPADDR in the local environment to the value found at the 5.11 file in Ipconfig.out file, type:

 setx IPADDR / f ipconfig.out / a 5.11 

To set the environment variable named OCTET1 in the local environment to the value found at 5.3 coordinates in Ipconfig.out file with "# $ *." Delimiters, type:

 setx OCTET1 / f ipconfig.out / a 5.3 / d "# $ *." 

To set an environment variable named IPGATEWAY in a local environment to a value found at 0.7 coordinates for the "Gateway" coordinates in Ipconfig.out file, enter:

 setx IPGATEWAY / f ipconfig.out / r 0.7 Gateway 

To display the contents of the file named Ipconfig.out - along with the corresponding coordinates of the content - on the computer named Computer1, enter:

 setx / s computer1 / u maindomhiropln / pp @ ssW23 / f ipconfig.out / x 

See more:

  1. Learn about the Windows Registry - Part I
  2. Good tip for the Windows Registry
  3. Summary of 20 common Run commands on Windows operating system
  4. Summary of shortcuts to know in Windows 10
4.3 ★ | 30 Vote