Skip to content

What is SETX Windows command?

The Windows setx command is used to set environment variables permanently in the Windows operating system. It allows users to create, modify, or delete environment variables using the command line interface. With setx, you can ensure that your environment variables persist across sessions and system reboots.

SETX Syntax:

Terminal window
setx [variable] [value] [/m]

SETX Options:

OptionDescription
/mSets the variable in the system environment space.

Parameters:

ParameterDescription
variableSpecifies the environment variable name to be set.
valueSpecifies the value to assign to the variable.

SETX Command Usage Examples:

Create a New Environment Variable

Terminal window
setx NEW_VAR "Hello, World"

Creates a new user environment variable named “NEW_VAR” with the value “Hello, World”.

Update an Existing Environment Variable

Terminal window
setx NEW_VAR "Hello, SETX" /M

Updates an existing system environment variable named “NEW_VAR” to the value “Hello, SETX”.

Delete an Environment Variable

Terminal window
setx OLD_VAR ""

Deletes the user environment variable named “OLD_VAR”.

Export a Specific Environment Variable to a File

Terminal window
setx PATH > path_backup.txt

Exports the user environment variable “PATH” and saves it to a file named “path_backup.txt”.

View the Value of an Environment Variable

Terminal window
setx NEW_VAR

Displays the current value of the user environment variable “NEW_VAR”.

How do I use setx in Windows?

To use the setx command in CMD, execute the following command:

Terminal window
setx variable value

What is the purpose of setx command in Windows?

The setx command is used to set environment variables permanently in Windows.

Terminal window
setx PATH "%PATH%;C:\New\Path"

How can I view the current environment variables after using setx?

To view the current environment variables after using setx, you can use the following command:

Terminal window
set

Can I use setx to create a new environment variable in Windows?

Yes, you can use setx to create a new environment variable in Windows by specifying the variable name and value.

Terminal window
setx NEWVAR "NewValue"

How do I overwrite an existing environment variable using setx?

To overwrite an existing environment variable using setx, simply use the same variable name with a new value.

Terminal window
setx existingVar "NewValue"

Is it possible to delete an environment variable with setx in Windows?

No, the setx command cannot be used to directly delete an environment variable. Instead, you can set it to an empty value to effectively remove it.

Terminal window
setx VAR ""

How do I set a system-wide environment variable using setx?

To set a system-wide environment variable using setx, you need to open CMD as an administrator and use the /M flag.

Terminal window
setx NEWVAR "NewValue" /M

Can I use setx to set a user-specific environment variable?

Yes, you can set a user-specific environment variable with setx by not using the /M flag, as it defaults to the current user.

Terminal window
setx NEWVAR "NewValue"

Applications of the SETX Command

  1. Setting system environment variables
  2. Creating or modifying user environment variables
  3. Automating batch scripts or commands
  4. Updating configuration settings
  5. Storing values for reuse in scripts