Home XP Commands
XP Syntax

SETX.exe (Resource Kit)

Set environment variables permanently

SETX can be used to set Environment Variables for the machine or currently logged on user:

   SETX Variable Value

   SETX Variable Value -m

Key:
     -m  Set the value in the Machine environment (HKLM)
         Default is User (HKCU)

SetX can also be used in modes to edit the Registry or edit CR-LF text files, (like win.ini) for most purposes these tasks are better done with other tools in the resource kit, e.g. the REG command.

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 command session.

Deleting variables
A 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. Either use the GUI (recommended) or delete the value from the registry with REG

REG delete HKCU\Environment /V _myvar

Deleting a variable in this way does not take effect until next logon due to caching of registry data. The type is REG_EXPAND_SZ.

Examples:

Set the variable "_mypc" to be COMPAQ in the users permanent environment:

SetX _mypc COMPAQ

Delete the variable "_myvar" in the users permanent environment:

REG delete HKCU\Environment /V _mypc

Set the variable "_myTimeZone" in both the immediate user session and the permanent environment:

SET _myTimeZone=GMT
SetX _myTimeZone GMT


Store the value of %my_important_var% in a second environment variable.
SetX _mybackupvar %my_important_var%

Sets the value of _mypath to ALWAYS be equal to the value of the %PATH% environment variable even in the event that the PATH variable changes:
SetX _mypath ~PATH~

Machine variables

These are stored on the machine and won't follow a users roaming profile.
To set a machine variable (-m) requires Administrator rights.

Create a machine variable:

SetX _myvar COMPAQ -m

Delete a machine variable:

REG delete HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment /V _myvar

"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 Commands:

SET - Display, set, or remove environment variables
REG - Delete keys or values from the registry
Q104011 - Modify variables by editing the Registry
SETENV - Vincent Fatica's improved version

Equivalent Linux BASH commands:

env - Display, set, or remove environment variables
export - Set an environment variable



Back to the Top

Simon Sheppard
SS64.com