Coming Soon: GET-IT: Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET Coming Soon: GET-IT: Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET
Windows Client OS

Create Environmental Variables With SetX

Overview

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.

Using 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.

​C:\> setx /? | more

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.

Sponsored Content

Say Goodbye to Traditional PC Lifecycle Management

Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.

​C:\> setx /s server01 Company "Acme Anvils" /m

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.

​C:\> setx MyLocation 9
C:\> setx asset_center 45632456 /m

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.

​C:\> setx /s server01 Company "Acme Anvils and Jetpacks" /m

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

​C:\> setx asset_center "" /m

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.

​C:\>setx /s server01 IE /k "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\IEXPLORE.EXE" /m

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.

​C:\>dir "%ie%"

Volume in drive C has no label.

Volume Serial Number is B0CE-F5BA

Directory of C:\Program Files\Internet Explorer

08/03/2011  03:15 PM           748,336 iexplore.exe

1 File(s)        748,336 bytes

0 Dir(s)  129,449,062,400 bytes free

C:\>

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.

​C:\> net config workstation > netconfig.txt

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.

​C:\>setx /f netconfig.txt /x

(0,0 Computer)(0,1 name)(0,2 \\QUARK)

(1,0 Full)(1,1 Computer)(1,2 name)(1,3 Quark)

(2,0 User)(2,1 name)(2,2 Jeff)

(4,0 Workstation)(4,1 active)(4,2 on)

(6,0 Software)(6,1 version)(6,2 Windows)(6,3 7)(6,4 Professional)

(8,0 Workstation)(8,1 domain)(8,2 JDHIT)

(9,0 Logon)(9,1 domain)(9,2 JDHIT)

(11,0 COM)(11,1 Open)(11,2 Timeout)(11,3 (sec))(11,4 0)

(12,0 COM)(12,1 Send)(12,2 Count)(12,3 (byte))(12,4 16)

(13,0 COM)(13,1 Send)(13,2 Timeout)(13,3 (msec))(13,4 250)

(14,0 The)(14,1 command)(14,2 completed)(14,3 successfully.)

C:\>

The coordinates I want are 9,2.

​C:\> setx LOGONDOMAIN /f netlogon.txt /a 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.

Conclusion

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:

BECOME A PETRI MEMBER:

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

Register
Comments (1)

One response to “Create Environmental Variables With SetX”

Leave a Reply

Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: