How Can I Append a Location to the Path Environment Variable?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I append a location to the Path environment variable?

— AC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AC. You know, there’s a TV commercial out in which a guy is watching a baseball game when his wife suddenly decides that the two of them should do something else. Talk about a dilemma: after all, according to the narrator, “In life there’s only room for one great passion … unless you’re really clever.” So does this guy do something really clever to solve his problem? You bet he does: giving we TV viewers a sly look, he uses his VCR to tape the baseball game.

We know: we never would have thought about using a video cassette recorder to record video, either. But that’s the sort of thing that separates the Really Clever Guys from the Scripting Guys.

Note. Well, that and the fact that – with at least one Scripting Guy – you never interrupt him when he’s watching a baseball game. You know the old advice about never trying to take a dog’s food while it’s eating? The same thing applies to this Scripting Guy and baseball.

Fortunately, though, you don’t have to be really clever to add a location to your Path variable. Instead, you just have to know how to use the WMI class Win32_Environment:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colItems = objWMIService.ExecQuery _ (“Select * From Win32_Environment Where Name = ‘Path'”)

For Each objItem in colItems strPath = objItem.VariableValue & “;C:\Scripts\” objItem.VariableValue = strPath objItem.Put_ Next

You’re right: this script is so simple it probably will leave you time for 2 or 3 additional great passions in life, won’t it? As you can see, we start out by connecting to the WMI service on the local computer. Of course, we aren’t limited to modifying the Path on the local computer. Instead, we can get the script to modify the Path on a remote computer simply by assigning the name of that computer to the variable strComputer:

strComputer = “atl-fs-01”

After making the connection to the WMI service we then use this line of code to retrieve a collection of all the environment variables that have the name Path:

Set colItems = objWMIService.ExecQuery _
    (“Select * From Win32_Environment Where Name = ‘Path'”)

Could we retrieve other environment variables using this type of query? You bet. For example, this query returns information about the environment variable Comspec:

Set colItems = objWMIService.ExecQuery _
    (“Select * From Win32_Environment Where Name = ‘Comspec'”)

Or, leave out the Where clause and get back information about all the environment variables on the computer:

Set colItems = objWMIService.ExecQuery _
    (“Select * From Win32_Environment”)

After retrieving the collection we set up a For Each loop to walk us through each item in that collection. (And don’t worry, that won’t take too long: because environment variable names must be unique we’ll only have, at most, one variable named Path.) Inside the loop we then execute these three lines of code:

strPath = objItem.VariableValue & “;C:\Scripts\”
objItem.VariableValue = strPath
objItem.Put_

What’s going on here? Well, in the first line we’re appending a new value to the VariableValue property and assigning the resulting string to strPath; as you probably guessed, VariableValue contains the value of the environment variable. So what new value are we assigning strPath? We’re assigning it the existing value of the VariableValue property plus a semicolon (;) plus the new location we want added to the Path: C:\Scripts\. In other words, suppose this is the current value of the Path environment variable:

C:\Program Files\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;
C:\WINDOWS\system32\WindowsPowerShell\v1.0

After executing this line of code the variable strPath will contain this value, which is simply the old Path plus the new location:

C:\Program Files\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;
C:\WINDOWS\system32\WindowsPowerShell\v1.0;C:\Scripts\

Get the idea? All we did was add a semicolon (because individual locations in the Path are separated by semicolons) followed by the additional location.

In line 2 we assign the value of strPath to the VariableValue property. After we’ve done that all we have to do is call the Put_ method to officially write the value of strPath to the property and, in the process, add C:\Scripts\ to our Path. Like we said, not very clever. But definitely quick and easy.

Note. How can you verify that this actually worked? Here’s one easy way. After the script finishes running open up a new command window and type path. If the script worked you’ll see C:\Scripts\ at the very end of the output.

Incidentally, that TV commercial we mentioned happens to be for a pharmaceutical company and one of its most popular products. As part of the required disclaimer the commercial features a doctor who states, somewhat matter-of-factly, “If you should experience a sudden decrease in vision, stop taking the drug and call your doctor immediately.” Which simply means this: if you take one of these pills and go blind, you probably shouldn’t take another one to see if that will help you get your vision back.

In the interest of public safety we’d like to add a similar disclaimer to today’s column: if you should experience a sudden decrease in vision, stop reading this column and call your doctor immediately.

Either that, or turn the light on. Remember, if you’re in the northern hemisphere it gets darker earlier and earlier these days.

0 comments

Discussion is closed.

Feedback usabilla icon