Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/future4tech/public_html/wp-includes/functions.php on line 6114
SETX.exe – Windows CMD Command - Future4Tech

SETX.exe – Windows CMD Command

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.

The CMD shell will fail to read an environment variable if it contains more than 8,191 characters. The Command Prompt will also ignore any environment variables that are inherited from a parent process if they are longer than 8191 characters.

SETX can 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.

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.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *


Notice: ob_end_flush(): Failed to send buffer of zlib output compression (0) in /home/future4tech/public_html/wp-includes/functions.php on line 5464