Categories
Command Line Environment Variables Linux setx setx examples Windows

Setting Windows Environment Variables With SETX

User specific environment variables can be set from My computer -> properties -> advanced -> environment variables. But when you want to do it from script, you need another way. You could perhaps create a script and make shortcut to start menu -> propgrams -> startup. Use folder C:Documents and SettingsAll UsersStart MenuProgramsStartup, if you want to make it available for all users.

From the command line use setx.exe which is available in Windows 2003, XP, Vista, Windows 7 and up. With it you can set environment variables from scripts so that those settings are visible also outside of the executing script. To view setx help type:

setx /?

SETX can be used to set values in the environment of the machine or currently logged on user using one of three modes – command line, registry and file mode.

  • Command Line Mode: setx variable value [-m]
    Optional Switches:
    -m – Set value in the Machine environment. Default is User.
  • Registry Mode: setx variable -k hivekey…value
    Optional Switches:
    -m – Set value in the Machine environment. Default is User.
  • File Mode: setx variable -f file {-a x,y | -r x,y “string”} [-d d] [-x] [-m]
    Required Switches:
    -f <file> – Specify file name to use
    -a x,y – Specify absolute coordinates and offset
    -r x,y “string” – Specify coordinates and offset relative to string

    Optional Switches:
    -d ,: etc. – Specify additional delimiters
    -x – Displays file coordinates. Switches -a -r -e ignored
    -m – Set value in the Machine environment. Default is User.

SETX Examples
For the file type examples you must first create the file that you wish to parse by using “command > filename” i.e. ipconfig > ipconfig.out.

IMPORTANT: SETX writes variables to the master environment in the registry. Variables set using SETX are only available in future command windows and not in the current command window.

SETX Command Line Examples

  • SETX MACHINE DELL – Sets value of MACHINE to be DELL in the users environment.
  • SETX MACHINE “DELL COMPUTER” -m – Sets value of MACHINE to be “DELL COMPUTER” in the machine environment.
  • SETX MYPATH %PATH% – Sets the value of MYPATH to the CURRENT value of the PATH environment variable.
  • SETX MYPATH ~PATH~ – 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 Registry Examples

  • SETX TZONE -k HKEY_LOCAL_MACHINESystemCurrentControlSetControlTimeZoneInforationStandardName – Sets the value of TZONE to the above key ie. “Central Standard Time”
  • SETX BUILD -k “HKEY_LOCAL_MACHINESoftwareMicrosoftWindows NTCurrentVersionurrentBuildNumber” – Sets the value of BUILD to the current Windows NT build ie. “1314”
    Note: Quotes must be used because of the embedded space in “Windows NT”.

SETX File Examples

  • SETX VAR -f ipconfig.out -x – Displays the coordinates for the contents of the file “ipconfig.out”.
  • SETX IPADDR -f ipconfig.out -a 5,11 – Finds value at absolute offset 5,11 of the file ipconfig.out and sets IPADDR to the IP Address (absolute offset 5,11).
  • SETX OCTET1 -f ipconfig.out -a 5,3 -d – Finds value at absolute offset 5,3 and uses “.” as an additional delimiter.  Sets OCTET1 to the first octet of the IP Address
  • SETX IPGATEWAY -f ipconfig.out -r 0,7 “Gateway” – Finds value at relative offset 0,7 to the keyword “Gateway” and sets IPGATEWAY to the first octet of the IP Address
Categories
Environment Variables Linux Windows Windows 2000

What environment variables are available in Windows?

List of the environment variables callable in windows 2000. e.g. Open a cmd prompt and type echo %appdata% which should return the full path to your profile’s Application Data directory. If calling from a batch file remember to quote the %variable% e.g.

IF EXIST “%appdata%”workravehistorystats. (mkdir D:AllMyFilesWorkrave.) ELSE echo Workrave. missing

Here’s the list with correct syntax:

ALLUSERSPROFILE %ALLUSERSPROFILE% Local returns the location of the All Users Profile.
APPDATA %APPDATA% Local returns the location where applications store data by default.
CD %CD% Local returns the current directory string.
CMDCMDLINE %CMDCMDLINE% Local returns the exact command line used to start the current cmd.exe.
CMDEXTVERSION %CMDEXTVERSION% System returns the version number of the current Command Processor Extensions.
COMPUTERNAME %COMPUTERNAME% System returns the name of the computer.
COMSPEC %COMSPEC% System returns the exact path to the command shell executable.
DATE %DATE% System returns the current date. This variable uses the same format as the date /t command. Cmd.exe generates this variable. For more information about the date command, see the Date command.
ERRORLEVEL %ERRORLEVEL% System returns the error code of the most recently used command. A non-0 value usually indicates an error.
HOMEDRIVE %HOMEDRIVE% System returns which local workstation drive letter is connected to the user’s home directory. This variable is set based on the value of the home directory. The user’s home directory is specified in Local Users and Groups.
HOMEPATH %HOMEPATH% System returns the full path of the user’s home directory. This variable is set based on the value of the home directory. The user’s home directory is specified in Local Users and Groups.
HOMESHARE %HOMESHARE% System returns the network path to the user’s shared home directory. This variable is set based on the value of the home directory. The user’s home directory is specified in Local Users and Groups.
LOGONSERVER %LOGONSERVER% Local returns the name of the domain controller that validated the current logon session.
NUMBER_OF_PROCESSORS %NUMBER_OF_PROCESSORS% System specifies the number of processors installed on the computer.
OS %OS% System returns the OS name. Windows XP and Windows 2000 display the OS as Windows_NT.
PATH %PATH% System specifies the search path for executable files.
PATHEXT %PATHEXT% System returns a list of the file extensions that the OS considers to be executable.
PROCESSOR_ARCHITECTURE %PROCESSOR_ARCHITECTURE% System returns the processor’s chip architecture. Values: x86, IA64.
PROCESSOR_IDENTIFIER %PROCESSOR_IDENTIFIER% System returns a description of the processor.
PROCESSOR_LEVEL %PROCESSOR_LEVEL% System returns the model number of the computer’s processor.
PROCESSOR_REVISION %PROCESSOR_REVISION% System returns the revision number of the processor.
Program Files %PROGRAMFILES% returns the location of the default install directory for applications.
PROMPT %PROMPT% Local returns the command-prompt settings for the current interpreter. Cmd.exe generates this variable.
RANDOM %RANDOM% System returns a random decimal number between 0 and 32767. Cmd.exe generates this variable.
SYSTEMDRIVE %SYSTEMDRIVE% System returns the drive containing the Windows root directory (i.e., the system root).
SYSTEMROOT %SYSTEMROOT% System returns the location of the Windows root directory.
TEMP %TEMP% System and User return the default temporary directories for applications that are available to users who are currently logged on. Some applications require TEMP and others require TMP.
TMP %TMP% System and User return the default temporary directories for applications that are available to users who are currently logged on. Some applications require TEMP and others require TMP.
TIME %TIME% System returns the current time. This variable uses the same format as the time /t command. Cmd.exe generates this variable. For more information about the time command, see the Time command.
USERDOMAIN %USERDOMAIN% Local returns the name of the domain that contains the user’s account.
USERNAME %USERNAME% Local returns the name of the user currently logged on.
USERPROFILE %USERPROFILE% Local returns the location of the profile for the current user.
WINDIR %WINDIR% System returns the location of the OS directory