Create Environmental Variables With SetX


Even though it has been around since forever, environmental variables continue to play an important role in the Windows operating system. You will still find applications, even the operating system, looking for values like %WINDIR% and %USERNAME%. Many IT Pros add their own environmental variables to support internal applications or automation processes. Configuring new environmental variables on remote machines can be a difficult and time consuming chore, or you can use a command tool like SETX.EXE.


You can find SETX.EXE on your Windows 7 desktop, which is where I’m assuming you are doing most of your management from. Open an elevated command prompt with administrator credentials and look at help.

There’s a lot here so I’m piping to the MORE command. You can use this tool to set environmental variables locally or on remote computers, assuming you have admin credentials. You can create either USER environmental variables or MACHINE environmental variables. The default is USER, using whatever credentials you are running under. I’m going to assume you want to set per machine variables.

The simplest syntax is to specify the variable name and a value.

This variable should be available the next time someone logs on to SERVER01. If you are running commands like this locally, they will be available in the next console window you open.

The first command will be a user specific variable and the second will be a system variable. Changing variables is as simple as running the command and assigning a new value.

If you want to delete a variable, simply assign a null value.

You can also create environmental variables from registry keys. Specify the registry key from either HKCU or HKLM and SETX will assign the value to your new variable. Registry values must be string, DWord, expandable string or multi string.

I’ve created a new environmental variable called IE that takes the value from this registry path. When I open up a new CMD window, I can use it.

The last, and very interesting way, to use SETX is to pull values from a text file. What is intriguing is that the text file can be in any format. Suppose I’ve created a text file from running the NET CONFIG Workstation cmd.

I want to grab the logon domain setting and turn that into an environmental variable. SETX will parse the text file into a set of coordinates. You’ll need to run a command like this to discover them.

The coordinates I want are 9,2.

Unfortunately, you can only grab a single value. But you can also find strings relative to another string. I’ll let you experiment with that feature.


There is much you can accomplish with SETX and if you aren’t afraid to get your hands dirty with a little batch file scripting, there’s really no limit. Hope you find it useful!

Related Topics:

  • Windows Client OS

    Don't have a login but want to join the conversation? Sign up for a Petri Account