{"id":63633,"date":"2007-11-08T00:29:00","date_gmt":"2007-11-08T00:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/11\/08\/hey-scripting-guy-how-can-i-remove-a-value-from-the-path-environment-variable\/"},"modified":"2007-11-08T00:29:00","modified_gmt":"2007-11-08T00:29:00","slug":"hey-scripting-guy-how-can-i-remove-a-value-from-the-path-environment-variable","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-remove-a-value-from-the-path-environment-variable\/","title":{"rendered":"Hey, Scripting Guy! How Can I Remove a Value From the Path Environment Variable?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! How can I remove a value from the Path environment variable?<BR><BR>&#8212; MG<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, MG. You know, today is Wednesday, November 7<SUP>th<\/SUP>, the day before the Scripting Guys head for Barcelona and <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/itpreview.mspx\"><B>Tech IT Forum<\/B><\/A>. 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, \u201cHey, do you know what <I>else<\/I> we should do before we go?\u201d<\/P>\n<TABLE id=\"EID\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. In case you\u2019re wondering, yes, Jean <I>did<\/I> tell Greg exactly what he should do before they go. Needless to say, however, he wasn\u2019t all that keen on giving that a try.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So why <I>are<\/I> the Scripting Guys running around so frantically rather than kicking back and enjoying the last day before their trip? That\u2019s easy: because the Scripting Guys are idiots. Instead of looking upon this trip as an all-expenses paid boondoggle (as any <I>sane<\/I> 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\u2019ll be up late each night in order to issue daily reports from the conference itself. For another, they\u2019ve also gone to great lengths to put together a wealth of new articles and new downloads, enough to make sure that there\u2019s something new and exciting posted to the Script Center every day next week. What <I>kind<\/I> of new and exciting things? Well, we hate to spoil the surprise. But we\u2019ll give you one hint: if you\u2019re intrigued by the recent announcement regarding <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/newin2.mspx\"><B>Windows PowerShell 2.0<\/B><\/A> then you\u2019ll definitely want to come back to the Script Center on Monday.<\/P>\n<P>You\u2019ll also want to come back on Monday if you want one last chance to win a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/bobbles.mspx\"><B>Dr. Scripto bobblehead<\/B><\/A>; after all, that\u2019s the day that we unveil the great TechEd IT Forum Challenge.<\/P>\n<P>And no, we aren\u2019t 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.<\/P>\n<P>Well, OK: you know how the Scripting Guys feel about <I>other<\/I> people cheating.<\/P>\n<P>Of course, all that fun and excitement is scheduled for next week. As far as <I>this<\/I> 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. <\/P>\n<P>Oops; never mind. Looks like we already came up with one:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Environment Where Name = &#8216;Path'&#8221;)<\/p>\n<p>For Each objItem in colItems\n    strPath = objItem.VariableValue\n    strPath = Replace(strPath, &#8220;;C:\\Test&#8221;, &#8220;&#8221;)\n    objItem.VariableValue = strPath\n    objItem.Put_\nNext\n<\/PRE>\n<P>So how exactly <I>do<\/I> 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:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-001&#8221;\n<\/PRE>\n<P>After that, we use the <B>ExecQuery<\/B> method to return a collection of all the environment variables that have a <B>Name<\/B> property equal to <I>Path<\/I>:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Environment Where Name = &#8216;Path'&#8221;)\n<\/PRE>\n<P>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.<\/P>\n<P>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\u2019s <B>VariableValue<\/B> property and store it in a variable named strPath:<\/P><PRE class=\"codeSample\">strPath = objItem.VariableValue\n<\/PRE>\n<P>In case you\u2019re wondering, that\u2019s going to give strPath a value similar to this. Look closely, and you\u2019ll see the target folder C:\\Test (the value we want to remove from the Path) sitting there about midway through the value:<\/P><PRE class=\"codeSample\">D:\\Perl\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;\nC:\\Program Files\\ATI Technologies\\ATI Control Panel;C:\\Program Files\\HPQ\\IAM\\bin;\nC:\\Program Files\\Microsoft SQL Server\\90\\Tools\\binn\\;C:\\Scripts;C:\\Test;\nC:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0;C:\\Program Files\\Common Files\\Adobe\\AGL;\nC:\\Program Files\\QuickTime\\QTSystem\n<\/PRE>\n<P>That\u2019s nice. But how exactly do we get <I>rid<\/I> of the target folder? That\u2019s another easy one; after all, that\u2019s what <I>this<\/I> line of code is for:<\/P><PRE class=\"codeSample\">strPath = Replace(strPath, &#8220;;C:\\Test&#8221;, &#8220;&#8221;)\n<\/PRE>\n<P>Here we\u2019re using the <B>Replace<\/B> function to search through the Path (or, more technically, the variable strPath), locate the value <I>;C:\\Test<\/I>, and then replace it with, well, nothing. (Or, to be a little more precise, replace it with an empty string.) Note that we\u2019re searching for the value <I>;C:\\Test<\/I>, 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\u2019d end up with double semicolons in the path:<\/P><PRE class=\"codeSample\">C:\\Scripts;;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0;\n<\/PRE>\n<P>Could that cause a problem? To tell you the truth, we weren\u2019t sure. So we decided to play it safe and avoid back-to-back semicolons.<\/P>\n<P>Of course, there <I>is<\/I> one potential problem here: if C:\\Test occurs at the very beginning of the Path it won\u2019t <I>have<\/I> a semicolon in front of it. In that case, our Replace command won\u2019t replace anything at all. Consequently, you might want to use a block of code similar to this instead:<\/P><PRE class=\"codeSample\">If Left(strPath, 8) = &#8220;C:\\Test;&#8221; Then\n    strPath = Replace(strPath, &#8220;C:\\Test;&#8221;, &#8220;&#8221;)\nElse\n    strPath = Replace(strPath, &#8220;;C:\\Test&#8221;, &#8220;&#8221;)\nEnd If\n<\/PRE>\n<P>All we\u2019re doing here is checking to see if the first eight characters in the path happen to be <I>C:\\Test;<\/I>. If they are, then <I>those<\/I> are the characters we replace with the empty string; if not, then we go ahead and replace the characters <I>;C:\\Test<\/I>.<\/P>\n<P>Just to be on the safe side.<\/P>\n<P>Regardless, our next two steps are to assign the modified value of strPath to the VariableValue property, then use the <B>Put_<\/B> method to officially write those changes to the actual Path environment variable. That\u2019s what we do with these two lines of code:<\/P><PRE class=\"codeSample\">objItem.VariableValue = strPath\nobjItem.Put_\n<\/PRE>\n<P>And when that\u2019s done, well, then we\u2019re done, too. At least as far as this particular script is concerned.<\/P>\n<P>Just a reminder, if you happen to find yourself in Barcelona next week be sure and stop by the Ask the Experts Pavilion (<A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/itpreview.mspx\"><B>booth 22<\/B><\/A>) and say hi; in return, Jean and Greg will give you a copy of <I>Dr. Scripto\u2019s Fun Book<\/I>; tell you what Scripting Guys Dean Tsaltas and Peter Costantini are <I>really<\/I> like; and give you a chance to win a Dr. Scripto bobblehead doll to boot. And if you <I>won\u2019t<\/I> find yourself in Barcelona next week and <I>can\u2019t<\/I> drop by and say hi, well, too bad for you; next year you\u2019ll know better, won\u2019t you?<\/P>\n<P>No, hey, just kidding. Trust us: we know how often people do \u2013 or, more correctly, <I>don\u2019t<\/I> \u2013 get to travel to conferences and seminars. So don\u2019t worry: if you can\u2019t 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\u2019ll see you all next week, one way or the other.<\/P>\n<P>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. <\/P>\n<P>Wow, a highwayman\u2019s hitch <I>and<\/I> a Spanish bowline! If we didn\u2019t know better, we might think that that she <I>wanted<\/I> us to stay tied up.<\/P>\n<TABLE id=\"EMAAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Ed<\/B><B>itor\u2019s Note:<\/B> 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\u2019t?<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I remove a value from the Path environment variable?&#8212; 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,47,3,5],"class_list":["post-63633","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I remove a value from the Path environment variable?&#8212; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=63633"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63633\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=63633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}