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):

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:

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

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:

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:

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 *