{"id":66593,"date":"2006-08-28T15:33:00","date_gmt":"2006-08-28T15:33:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/28\/how-can-i-append-a-location-to-the-path-environment-variable\/"},"modified":"2006-08-28T15:33:00","modified_gmt":"2006-08-28T15:33:00","slug":"how-can-i-append-a-location-to-the-path-environment-variable","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-append-a-location-to-the-path-environment-variable\/","title":{"rendered":"How Can I Append a Location to the Path Environment Variable?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! How can I append a location to the Path environment variable?<BR><BR>&#8212; AC<\/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, AC. You know, there\u2019s 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, \u201cIn life there\u2019s only room for one great passion \u2026 unless you\u2019re <I>really<\/I> clever.\u201d So does this guy do something really clever to solve his problem? You bet he does: giving we TV viewers a sly look, he <I>uses his VCR to tape the baseball game<\/I>. <\/P>\n<P>We know: we never would have thought about using a video cassette recorder to record video, either. But that\u2019s the sort of thing that separates the Really Clever Guys from the Scripting Guys.<\/P>\n<TABLE id=\"EDD\" 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>. Well, that and the fact that \u2013 with at least one Scripting Guy \u2013 you <I>never<\/I> interrupt him when he\u2019s watching a baseball game. You know the old advice about never trying to take a dog\u2019s food while it\u2019s eating? The same thing applies to this Scripting Guy and baseball.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Fortunately, though, you don\u2019t have to be <I>really<\/I> clever to add a location to your Path variable. Instead, you just have to know how to use the WMI class Win32_Environment:<\/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 &amp; &#8220;;C:\\Scripts\\&#8221;\n    objItem.VariableValue = strPath\n    objItem.Put_\nNext\n<\/PRE>\n<P>You\u2019re right: this script is so simple it probably <I>will<\/I> leave you time for 2 or 3 additional great passions in life, won\u2019t it? As you can see, we start out by connecting to the WMI service on the local computer. Of course, we aren\u2019t 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:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>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 <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>Could we retrieve other environment variables using this type of query? You bet. For example, this query returns information about the environment variable <I>Comspec<\/I>:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Environment Where Name = &#8216;Comspec'&#8221;)\n<\/PRE>\n<P>Or, leave out the Where clause and get back information about all the environment variables on the computer:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Environment&#8221;)\n<\/PRE>\n<P>After retrieving the collection we set up a For Each loop to walk us through each item in that collection. (And don\u2019t worry, that won\u2019t take too long: because environment variable names must be unique we\u2019ll only have, at most, one variable named Path.) Inside the loop we then execute these three lines of code:<\/P><PRE class=\"codeSample\">strPath = objItem.VariableValue &amp; &#8220;;C:\\Scripts\\&#8221;\nobjItem.VariableValue = strPath\nobjItem.Put_\n<\/PRE>\n<P>What\u2019s going on here? Well, in the first line we\u2019re appending a new value to the <B>VariableValue<\/B> 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\u2019re assigning it the existing value of the VariableValue property <I>plus<\/I> a semicolon (;) <I>plus<\/I> the new location we want added to the Path: <B>C:\\Scripts\\<\/B>. In other words, suppose this is the current value of the Path environment variable:<\/P><PRE class=\"codeSample\">C:\\Program Files\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;\nC:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\n<\/PRE>\n<P>After executing this line of code the variable strPath will contain <I>this<\/I> value, which is simply the old Path plus the new location:<\/P><PRE class=\"codeSample\">C:\\Program Files\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;\nC:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0;C:\\Scripts\\\n<\/PRE>\n<P>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.<\/P>\n<P>In line 2 we assign the value of strPath to the VariableValue property. After we\u2019ve done that all we have to do is call the <B>Put_<\/B> 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.<\/P>\n<TABLE id=\"EIF\" 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>. How can you verify that this actually worked? Here\u2019s one easy way. After the script finishes running open up a new command window and type <B>path<\/B>. If the script worked you\u2019ll see C:\\Scripts\\ at the very end of the output.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>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, \u201cIf you should experience a sudden decrease in vision, stop taking the drug and call your doctor immediately.\u201d Which simply means this: if you take one of these pills and go blind, you probably shouldn\u2019t take another one to see if that will help you get your vision back.<\/P>\n<P>In the interest of public safety we\u2019d like to add a similar disclaimer to today\u2019s column: if you should experience a sudden decrease in vision, stop reading this column and call your doctor immediately. <\/P>\n<P>Either that, or turn the light on. Remember, if you\u2019re in the northern hemisphere it gets darker earlier and earlier these days.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I append a location to the Path environment variable?&#8212; AC Hey, AC. You know, there\u2019s 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 [&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-66593","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 append a location to the Path environment variable?&#8212; AC Hey, AC. You know, there\u2019s 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66593","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=66593"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66593\/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=66593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}