Hey, Scripting Guy! How can I remove a value from the Path environment variable?
— MG
Hey, MG. You know, today is Wednesday, November 7th, the day before the Scripting Guys head for Barcelona and Tech IT Forum. Even as we speak, Scripting Guy Jean Ross is running around frantically, trying to tie up all the loose ends before we go. Meanwhile, Scripting Guy Greg Stemp is helping out by saying, “Hey, do you know what else we should do before we go?”
Note. In case you’re wondering, yes, Jean did tell Greg exactly what he should do before they go. Needless to say, however, he wasn’t all that keen on giving that a try. |
So why are the Scripting Guys running around so frantically rather than kicking back and enjoying the last day before their trip? That’s easy: because the Scripting Guys are idiots. Instead of looking upon this trip as an all-expenses paid boondoggle (as any sane conference-goer would do) the Scripting Guys are trying to turn their journey into a full-fledged extravaganza. For one thing, that means that they’ll be up late each night in order to issue daily reports from the conference itself. For another, they’ve also gone to great lengths to put together a wealth of new articles and new downloads, enough to make sure that there’s something new and exciting posted to the Script Center every day next week. What kind of new and exciting things? Well, we hate to spoil the surprise. But we’ll give you one hint: if you’re intrigued by the recent announcement regarding Windows PowerShell 2.0 then you’ll definitely want to come back to the Script Center on Monday.
You’ll also want to come back on Monday if you want one last chance to win a Dr. Scripto bobblehead; after all, that’s the day that we unveil the great TechEd IT Forum Challenge.
And no, we aren’t going to tell you ahead of time what the TechEd IT Forum Challenge is. That would be cheating. And you know how the Scripting Guys feel about cheating.
Well, OK: you know how the Scripting Guys feel about other people cheating.
Of course, all that fun and excitement is scheduled for next week. As far as this week goes, we still have quite a few loose ends to tie up. Like what? Well, for one thing, we still have to come up with a script that can remove a value from the Path environment variable.
Oops; never mind. Looks like we already came up with one:
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 strPath = Replace(strPath, “;C:\Test”, “”) objItem.VariableValue = strPath objItem.Put_ Next
So how exactly do we remove something from the Path environment variable? Well, to begin with, we connect to the WMI service on the local computer. And yes, we can use this same script to modify the Path on a remote computer; all we have to do is assign the name of that remote computer to the variable strComputer:
strComputer = “atl-fs-001”
After that, we use the ExecQuery method to return a collection of all the environment variables that have a Name property equal to Path:
Set colItems = objWMIService.ExecQuery _ (“Select * From Win32_Environment Where Name = ‘Path'”)
Because environment variables must have unique names, that query is going to return a collection with just one such environment variable in it: the Path.
Of course, even a single item collection is still a collection; because of that, we need to set up a For Each loop to loop through each item in that collection. Inside that loop, we grab the value of the Path’s VariableValue property and store it in a variable named strPath:
strPath = objItem.VariableValue
In case you’re wondering, that’s going to give strPath a value similar to this. Look closely, and you’ll see the target folder C:\Test (the value we want to remove from the Path) sitting there about midway through the value:
D:\Perl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem; C:\Program Files\ATI Technologies\ATI Control Panel;C:\Program Files\HPQ\IAM\bin; C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Scripts;C:\Test; C:\WINDOWS\system32\WindowsPowerShell\v1.0;C:\Program Files\Common Files\Adobe\AGL; C:\Program Files\QuickTime\QTSystem
That’s nice. But how exactly do we get rid of the target folder? That’s another easy one; after all, that’s what this line of code is for:
strPath = Replace(strPath, “;C:\Test”, “”)
Here we’re using the Replace function to search through the Path (or, more technically, the variable strPath), locate the value ;C:\Test, and then replace it with, well, nothing. (Or, to be a little more precise, replace it with an empty string.) Note that we’re searching for the value ;C:\Test, which is the folder path plus a semicolon at the beginning. Why do we do that? Well, that way we end up with a nice, clean path. After all, if we simply removed C:\Test, we’d end up with double semicolons in the path:
C:\Scripts;;C:\WINDOWS\system32\WindowsPowerShell\v1.0;
Could that cause a problem? To tell you the truth, we weren’t sure. So we decided to play it safe and avoid back-to-back semicolons.
Of course, there is one potential problem here: if C:\Test occurs at the very beginning of the Path it won’t have a semicolon in front of it. In that case, our Replace command won’t replace anything at all. Consequently, you might want to use a block of code similar to this instead:
If Left(strPath, 8) = “C:\Test;” Then strPath = Replace(strPath, “C:\Test;”, “”) Else strPath = Replace(strPath, “;C:\Test”, “”) End If
All we’re doing here is checking to see if the first eight characters in the path happen to be C:\Test;. If they are, then those are the characters we replace with the empty string; if not, then we go ahead and replace the characters ;C:\Test.
Just to be on the safe side.
Regardless, our next two steps are to assign the modified value of strPath to the VariableValue property, then use the Put_ method to officially write those changes to the actual Path environment variable. That’s what we do with these two lines of code:
objItem.VariableValue = strPath objItem.Put_
And when that’s done, well, then we’re done, too. At least as far as this particular script is concerned.
Just a reminder, if you happen to find yourself in Barcelona next week be sure and stop by the Ask the Experts Pavilion (booth 22) and say hi; in return, Jean and Greg will give you a copy of Dr. Scripto’s Fun Book; tell you what Scripting Guys Dean Tsaltas and Peter Costantini are really like; and give you a chance to win a Dr. Scripto bobblehead doll to boot. And if you won’t find yourself in Barcelona next week and can’t drop by and say hi, well, too bad for you; next year you’ll know better, won’t you?
No, hey, just kidding. Trust us: we know how often people do – or, more correctly, don’t – get to travel to conferences and seminars. So don’t worry: if you can’t come to TechEd IT Forum then the Scripting Guys will help bring TechEd IT Forum to you; next week the Script Center will be awash in articles, daily reports, downloads, games, prizes, and who-knows-what. We’ll see you all next week, one way or the other.
Assuming we can untie ourselves, that is. When Jean said she had a few more things to tie up we assumed she meant that figuratively, not literally.
Wow, a highwayman’s hitch and a Spanish bowline! If we didn’t know better, we might think that that she wanted us to stay tied up.
Editor’s Note: Just to set the record straight, Jean has never tied up another Scripting Guy. She has been tempted to gag one or two of them, but really, who hasn’t? |
0 comments