Link to home
Create AccountLog in
Avatar of Kyle Olderog
Kyle OlderogFlag for United States of America

asked on

Update environment variables for multiple computers

In our environment we have changes to make somewhat often to environment variables (system variables).  For instance we need to update the PATH variable value from "U:\software_version1" to "U:\software_version2".

Also we have a variable called "LM_LICENSE_FILE" with a value of "1111@server1; 2222@server2; 3333@server3" that we would need to update to "1111@server1; 2222@server2; 4444@server4".

We have about 500 users and it is no good to go touch each machine to make these changes.

We have SMS or can use login scripts or group policy if possible.  We prefer to use SMS if possible.
Avatar of and235100
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
You can edit the "Path" name at this location, using reg add /f - or using vbscript as per my 2nd link.
Why not use a login or Startup Script?
How Can I Create an Environment Variable Using a Script?


Hey, Scripting Guy! Can I create an environment variable using a script?

-- OD


Hey, OD. WMI is a great technology for scripting, but it has its eccentricities. For example, can you create a process using a script? Of course you can; after all, WMIs Win32_Process class has a Create method. Can you create a service using a script? Of course you can; after all, WMIs Win32_Service class has a Create method. Can you create an environment variable using a script? Of course you can, despite the fact that the Win32_Environment class doesnt have a Create method. Strange but true!


In fact, heres a script that creates a new environment variable named TestValue:


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objVariable = objWMIService.Get("Win32_Environment").SpawnInstance_

objVariable.Name = "TestValue"
objVariable.UserName = "System"
objVariable.VariableValue = "This is a test"


We know: this looks different from most WMI scripts. But dont worry, were going to walk through the code, step-by-step.


Our environment variable script begins the same way most WMI scripts do: by connecting to the WMI service. After connecting to the WMI service you typically call ExecQuery to return a collection of items. However, were not going to do that with this script. Instead, were going to use the SpawnInstance_ method to create a blank environment variable, one that exists only in memory. (We use the term blank because SpawnInstance_ creates an environment variable that has all the properties of an environment variable; however, at the time of creation none of those properties are assigned a value. Hence a blank instance.)


This line of code creates an object reference (objVariable) that points to our new environment variable (which, again, exists only in memory when created):


Set objVariable = objWMIService.Get("Win32_Environment").SpawnInstance_


And, no, thats not a misprint: the name really isSpawnInstance_, with an underscore coming at the end. Youll see a lot of underscores before this column is finished.


What we need to do now is fill in the properties of our environment variable. In particular, we need to specify three values:


·         Name. This is simply the name of the environment variable. In this sample script we use the clever name TestValue.

·         UserName. This is the owner of the environment variable. To make this a system variable, set UserName to System; to make it a user-specific environment variable, set UserName to the user account name (for example, fabrikam\kenmyer).

·         VariableValue. The assigned value of the environment variable. Once again weve gone overboard with our cleverness, assigning TestValue the value This is a test.


And, yes, thats why we Scripting Guys get paid the big bucks.


With the three property values assigned, we then call the Put_ method, which actually writes the new environment variable to the operating system. Mission accomplished.


What do you mean, Are you sure? If you dont believe us, a quick way to verify creation is to use a script similar to this one. This script uses a WQL query to select all environment variables with the name TestValue, then echoes the property values of that variable to the screen:


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_Environment Where Name = 'TestValue'")

For Each objItem in colItems
    Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "User Name: " & objItem.UserName
    Wscript.Echo "Variable Value: " & objItem.VariableValue


Of course, whats the fun of having a brand-new, custom-made environment variable if you never get to play with it? If you want to change the value of your new environment variable, simply connect to that variable (using the same WQL query we just showed you) and assign a new value to the VariableValue property. Call the Put_ method to write the changes to the operating system, and your environment variable will take on the new value. For example, this sample script changes the value of TestValue to New value:


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_Environment Where Name = 'TestValue'")

For Each objItem in colItems
    objItem.VariableValue = "New value"


Childs play.


But what if you want to delete your environment variable? (Yes, even after all the work you did to create it.) Thats easy enough: we just take the same basic approach we used when modifying the value of the environment variable. This time, however, we dont assign a new value to the VariableValue property; instead we simply call the Delete_ method to delete the variable:


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_Environment Where Name = 'TestValue'")

For Each objItem in colItems


Again, note the underscore on the end of the method name: Delete_.


Of course, theres still one question left unanswered: how did we know that the Win32_Environment class supported the SpawnInstance_ and Delete_ methods? In this case, we used Wbemtest.exe. Call up Wbemtest and take a look at the Win32_Environment class:

If you look at the very top of the screen (under the Qualifiers heading) youll see the CreateBy and DeleteBy qualifiers, followed by the methods used to create a new instance (PutInstance) and delete an existing instance (DeleteInstance). Thats how we could tell that Win32_Environment supported SpawnInstance_ and Delete_.

I found that on Microsofts website for the Scripting Guys, as if you couldn't tell.
Yes - the same one I had already given a link to 9 minutes before yours.
However - I don't tend to copy and paste huge amounts of text from external websites - firstly it is a waste of time - and secondly - it makes the question look untidy for people to search for a solution through in the future.
Avatar of Kyle Olderog


So we need to get Visual Basic to compile this script into an .exe? Thanks for the quick replies.
Avatar of matrixnz

You can use SetX to set Machine Variables

Download and Install the Windows XP Service Pack 2 Support Tools

In C:\Program Files\Support Tools\ copy setx.exe to a folder share or where ever you wish to run your script then just use something like
setx.exe LM_LICENSE_FILE "1111@server1; 2222@server2; 4444@server4" -m

The -m places into the System Environment Variables remove it to place it in the Users

Sorry amd I was down at the comment area when you post come in so I didn't see it. At lease we are on the same sheet of music. ;-)
I am having one of those days again, sorry and235100 I spelled the simplest part of your name wrong above.
Thanks for your quick response, got it working!