Setx
Updated: January 21, 2005
Syntax
setx [/s Computer [/u [Domain\]User [/p [Password]]]] [Variable] /k Path [/m]
setx [/s Computer [/u [Domain\]User [/p [Password]]]] /f FileName {[Variable] {/a X,y | /r X,y "String"} [/m] | /x} [/d Delimiters]
Parameters
Remarks
- Using the /s, /u, and /p command-line options The /u and /p command-line options are available only when you use /s. You must use /p with /u to provide the user's password.
- Setx provides the only command-line or programmatic way to directly and permanently set system environment values. System environment variables are manually configurable through Control Panel or through a registry editor (Regedit.exe). The Set command, which is internal to the command interpreter (that is, Cmd.exe), sets user environment variables for the current console window only.
- You can use setx to set values for user and system environment variables from one of three sources: Command Line Mode, Registry Mode, or File Mode.
- Setx is similar to the UNIX utility SETENV.
- Setx writes variables to the master environment in the registry. Variables set with setx variables are available in future command windows only, not in the current command window.
- HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE are the only supported hives. REG_DWORD, REG_EXPAND_SZ, REG_SZ AND REG_MULTI_SZ are the valid RegKey data types.
- When gaining access to REG_MULTI_SZ values in the registry, only the first item is extracted and used.
- You cannot use setx to remove values added to the local or system environments. You can use set with a variable name and no value to remove a corresponding value from the local environment.
- REG_DWORD registry values are extracted and used in hexadecimal mode
- File mode supports the parsing of CR-LF text files only.
Examples
setx MACHINE brand1
To set the MACHINE environment variable in the system environment to the value Brand1 Computer, type:
setx MACHINE "brand1 computer" /m
To set the MYPATH environment variable in the local environment to use the search path defined in the PATH environment variable, type:
setx MYPATH %PATH%
To set the MYPATH environment variable in the local environment to use the search path defined in the PATH environment variable after replacing ~ with % , type:
setx MYPATH ~PATH~
To set the MACHINE environment variable in the local environment to BRAND1 on a computer named Computer1, type:
setx /s computer1 /u maindom\hiropln /p p@ssW23 MACHINE BRAND1
To set the MYPATH environment variable in the local environment to use the search path defined in the PATH environment variable on a computer named Computer1, type:
setx /s computer1 /u maindom\hiropln /p p@ssW23 MYPATH %PATH%
To set the TZONE environment variable in the local environment to the value found in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ TimeZoneInformation\StandardName registry key, type:
setx TZONE /k HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ TimeZoneInformation\StandardName
To set the BUILD environment variable in the system environment to the value found in the HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\ CurrentVersion\CurrentBuildNumber registry key, type:
setx BUILD /k "HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\ CurrentVersion\CurrentBuildNumber" /m
To set the TZONE environment variable in the local environment on a computer named Computer1 to the value found in the HKEY_LOCAL_MACHINE\ System\CurrentControlSet\Control\ TimeZoneInformation\StandardName registry key, type:
setx /s computer1 /u maindom\hiropln /p p@ssW23 TZONE /k HKEY_LOCAL_MACHINE\ System\CurrentControlSet\Control\ TimeZoneInformation\StandardName
To set the BUILD environment variable in the system environment on a computer named Computer1 to the value found in the HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\ CurrentVersion\CurrentBuildNumber registry key, type:
setx /s computer1 /u maindom\hiropln /p p@ssW23 BUILD /k "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber" /m
To display the contents and their corresponding coordinates of a file ipconfig.out, type:
setx /f ipconfig.out /x
To set the IPADDR environment variable in the local environment to the value found at the coordinate 5,11 in the file ipconfig.out, type:
setx IPADDR /f ipconfig.out /a 5,11
To set the OCTET1 environment variable in the local environment to the value found at the coordinate 5,3 in the file ipconfig.out with delimiters "#$*.", type:
setx OCTET1 /f ipconfig.out /a 5,3 /d "#$*."
To set the IPGATEWAY environment variable in the local environment to the value found at the coordinate 0,7 with respect to the coordinate of "Gateway" in the file ipconfig.out, type:
setx IPGATEWAY /f ipconfig.out /r 0,7 Gateway
To display the contents and their corresponding coordinates of a file ipconfig.out on a computer named Computer1, type:
setx /s computer1 /u maindom\hiropln /p p@ssW23 /f ipconfig.out /x
Formatting legend
Format | Meaning |
Italic | Information that the user must supply |
Bold | Elements that the user must type exactly as shown |
Ellipsis (...) | Parameter that can be repeated several times in a command line |
Between brackets ([]) | Optional items |
Between braces ({}); choices separated by pipe (|). Example: {even|odd} | Set of choices from which the user must choose only one |
Courier font | Code or program output |