How To Use The Setx Command In Windows (2026)
Setx saves an environment variable permanently, so it is still there the next time you open a command window.
The basic form is setx <variable> <value> for your own account and setx <variable> <value> /m for the whole computer.
This guide follows Microsoft’s setx reference, which lists Windows 11 and Windows 10 as supported.
Table of Contents
- Setx quick answers
- What setx does
- Setx versus set
- Open Command Prompt
- The setx syntax
- Setx switches at a glance
- Set a variable for your account
- Set a variable for the whole computer
- Copy a registry value into a variable
- File mode and coordinates
- Set a variable on another computer
- Using other variables as the value
- The 1,024 character limit
- Be careful with PATH
- Removing a variable
- Common setx mistakes
- Save a variable step by step
- Related guides
- Official sources
- More Windows guides
Setx quick answers
| I want to | Type this |
|---|---|
| Set a variable for my account | setx MACHINE Brand1 |
| Set a variable for the whole computer | setx MACHINE "Brand1 Computer" /m |
| Copy a registry value into a variable | setx BUILD /k HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\CurrentBuildNumber /m |
| Show help | setx /? |
Open a new Command Prompt after running setx, because the current window does not see the change.
What setx does
Microsoft says setx creates or modifies environment variables in the user or system environment, without needing programming or scripting.
It can also read the value of a registry key and write it into a variable.
Microsoft calls it the only command-line or programmatic way to directly and permanently set system environment values.
Setx versus set
| Command | How long the value lasts |
|---|---|
set | Only in the current console window, because it is internal to Cmd.exe. |
setx | Saved to the master environment in the registry, so it is available in future command windows only. |
That second row is the reason a variable you just saved with setx looks empty in the same window.
Open Command Prompt
- Press the Windows key and type cmd.
- Press Enter to open Command Prompt.
- For a system-wide change with /m, right-click Command Prompt and choose Run as administrator.
The administrator steps are in how to open Terminal as administrator.
The setx syntax
Microsoft documents three modes: command line, registry and file.
Command line mode is setx [/s <computer> [/u [<domain>\]<user name> [/p [<password>]]]] <variable> <value> [/m].
Registry mode is setx [...] <variable> /k <path> [/m], where the path looks like HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName.
File mode reads a value from a text file with /f <filename> and the coordinate options.
Setx switches at a glance
| Switch | What it does |
|---|---|
<variable> <value> | The variable name and the value to store. |
/m | Sets the variable in the system environment, and the default is the local environment. |
/k <path> | Sets the variable from a registry key path. |
/s <computer> | Names a remote computer by name or IP address, without backslashes. |
/u | Runs the command with the credentials of another user account. |
/p | Gives the password for the account named in /u. |
/f <filename> | Chooses the file to read in file mode. |
/a <X>,<Y> | Gives absolute coordinates as search parameters. |
/r <X>,<Y> <String> | Gives coordinates relative to a string. |
/x | Displays file coordinates and ignores /a, /r and /d. |
/d <delimiters> | Adds delimiters beyond SPACE, TAB, ENTER and LINEFEED, up to 15 in total. |
Set a variable for your account
- Open Command Prompt.
- Run
setx MACHINE Brand1, which is Microsoft’s example for the local environment. - Close the window and open a new Command Prompt.
- Type
echo %MACHINE%to see the saved value.
The echo check is a standard way to read a variable and is not part of Microsoft’s setx page.
Set a variable for the whole computer
Add /m to write the value to the system environment.
Microsoft’s example is setx MACHINE Brand1 Computer /m, which stores Brand1 Computer.
Use an elevated Command Prompt for this, since a system-wide change affects every account.
Copy a registry value into a variable
Use /k with a registry path to fill a variable from a registry key.
Microsoft’s example sets TZONE to the StandardName time zone value with setx TZONE /k HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName.
Only HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE are supported hives.
The valid data types are REG_DWORD, REG_EXPAND_SZ, REG_SZ and REG_MULTI_SZ, and for a REG_MULTI_SZ value only the first item is used.
File mode and coordinates
File mode pulls a value out of a text file by its position.
Microsoft says file mode supports only carriage return and line feed (CRLF) text files.
| Goal | Microsoft example |
|---|---|
| Show a file with its coordinates | setx /f ipconfig.out /x |
| Take the value at coordinate 5,11 | setx IPADDR /f ipconfig.out /a 5,11 |
| Take 5,3 using extra delimiters | setx OCTET1 /f ipconfig.out /a 5,3 /d #$*. |
| Take a value relative to the word Gateway | setx IPGATEWAY /f ipconfig.out /r 0,7 Gateway |
Set a variable on another computer
The /s switch points setx at a remote computer, and /u runs it under a different account.
Microsoft’s page shows a password after /p in its examples, but typing a password into a command line leaves it in plain sight on screen.
Home users almost never need these switches, because the default target is the local computer.
Using other variables as the value
Microsoft shows setx MYPATH %PATH% to set MYPATH from the search path defined in PATH.
It also shows setx MYPATH ~PATH~, which Microsoft describes as setting MYPATH to use PATH after replacing the tildes with percent signs.
The 1,024 character limit
Microsoft warns there is a limit of 1,024 characters when assigning contents to a variable with setx.
Anything over the limit is cropped, and the cropped text is what gets applied.
If that cropped text lands on an existing variable, data it held before can be lost.
Be careful with PATH
Running setx on an existing variable removes variable references and stores the expanded values.
Microsoft’s example is a PATH that refers to %JAVADIR%, which setx expands so later changes to %JAVADIR% are not reflected in PATH.
Combined with the 1,024 character limit, this is why the safer route for PATH is the Settings screen in how to edit environment variables and PATH.
Removing a variable
Microsoft says you cannot use setx to remove values added to the local or system environments.
It also says you can use setx with a variable name and no value to remove the corresponding value from the local environment.
Because those two statements pull in different directions, check the result in a new window before relying on it.
Common setx mistakes
- Reading the variable in the same window and thinking setx failed.
- Leaving out /m when you wanted a system-wide value.
- Pasting a long PATH value that goes over 1,024 characters.
- Putting a password in the command line when using /p.
- Using /d or /a without the /f file mode they belong to.
Save a variable step by step
- Open Command Prompt, as administrator if you will use /m.
- Run
setx <name> "<value>"and watch for a success message. - Close the window and open a new one.
- Check the value with echo before starting the program that needs it.
Related guides
Programs that read these variables include Java and Node.js and npm.
For editing the registry directly, see the reg add command guide.
Official sources
The syntax, switches, limits and examples come from Microsoft’s setx reference.
More Windows guides
- How To Uninstall ESET From Windows Step By Step (2026)
- How To Uninstall Kaspersky Completely From Windows (2026)
- Local Security Authority Protection Is Off In Windows 11 (2026)
- How To Use Ping And Tracert In Windows 11 (2026)
- How To Change UAC Settings In Windows 11 (2026)
- How To Rip A CD To Your PC In Windows 11 (2026)
See all: Windows 11 Guides