|

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

I want toType this
Set a variable for my accountsetx MACHINE Brand1
Set a variable for the whole computersetx MACHINE "Brand1 Computer" /m
Copy a registry value into a variablesetx BUILD /k HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\CurrentBuildNumber /m
Show helpsetx /?

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

CommandHow long the value lasts
setOnly in the current console window, because it is internal to Cmd.exe.
setxSaved 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

  1. Press the Windows key and type cmd.
  2. Press Enter to open Command Prompt.
  3. 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

SwitchWhat it does
<variable> <value>The variable name and the value to store.
/mSets 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.
/uRuns the command with the credentials of another user account.
/pGives 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.
/xDisplays 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

  1. Open Command Prompt.
  2. Run setx MACHINE Brand1, which is Microsoft’s example for the local environment.
  3. Close the window and open a new Command Prompt.
  4. 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.

GoalMicrosoft example
Show a file with its coordinatessetx /f ipconfig.out /x
Take the value at coordinate 5,11setx IPADDR /f ipconfig.out /a 5,11
Take 5,3 using extra delimiterssetx OCTET1 /f ipconfig.out /a 5,3 /d #$*.
Take a value relative to the word Gatewaysetx 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

  1. Open Command Prompt, as administrator if you will use /m.
  2. Run setx <name> "<value>" and watch for a success message.
  3. Close the window and open a new one.
  4. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *