{"id":68273,"date":"2005-12-22T22:00:00","date_gmt":"2005-12-22T22:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/12\/22\/how-can-i-retrieve-the-path-to-the-program-files-folder-on-a-computer\/"},"modified":"2005-12-22T22:00:00","modified_gmt":"2005-12-22T22:00:00","slug":"how-can-i-retrieve-the-path-to-the-program-files-folder-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-retrieve-the-path-to-the-program-files-folder-on-a-computer\/","title":{"rendered":"How Can I Retrieve the Path to the Program Files Folder on a Computer?"},"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 retrieve the path to the Program Files folder on a computer?<BR><BR>&#8212; CC<\/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, CC. How can you retrieve the path to the Program Files folder on a computer? Well, to be honest, not as easily as we <I>thought<\/I> you could. If you\u2019re trying to retrieve the Program Files path on the local computer, well, that\u2019s easy; retrieving the Program Files path on a remote machine turned out to be a bit of a sticky wicket. But, we <I>did<\/I> find a way to do this.<\/P>\n<P>Let\u2019s start by looking at the easy scenario. Here\u2019s a script that uses the <B>Shell<\/B> object to retrieve the path to the Program Files folder on the local computer:<\/P><PRE class=\"codeSample\">Const PROGRAM_FILES = &amp;H26&amp;<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(PROGRAM_FILES)\nSet objFolderItem = objFolder.Self\nWscript.Echo objFolderItem.Path\n<\/PRE>\n<P>The script begins by defining a constant named PROGRAM_FILES and setting the value to &amp;H26&amp;; we\u2019ll use this constant to tell the script which \u201cspecial folder\u201d we want to work with. (Are there <I>other<\/I> special folders we can work with? You bet there are, and the Script Center <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/desktop\/special\/default.mspx\"><B>Script Repository<\/B><\/A> has a few dozen scripts that show how to do this.) We create an instance of the <B>Shell.Application<\/B> object, and then use the <B>Namespace<\/B> method to bind to the Program Files folder (note that we pass the constant PROGRAM_FILES to the Namespace method):<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(PROGRAM_FILES)\n<\/PRE>\n<P>Now we encounter one of the little idiosyncrasies of the Shell object. Although we have bound ourselves to the <B>Folder<\/B> object that represents the Program Files folder, we can\u2019t directly retrieve the path to the folder. Instead, we have to create a <B>FolderItemObject<\/B> for the folder; that\u2019s something we can do using the <B>Self<\/B> method (which basically just binds the folder to itself):<\/P><PRE class=\"codeSample\">Set objFolderItem = objFolder.Self\n<\/PRE>\n<P>Weird, but once we have a FolderItemObject we can echo back the value of the <B>Path<\/B> property:<\/P><PRE class=\"codeSample\">Wscript.Echo objFolderItem.Path\n<\/PRE>\n<P>As you might expect, the value of the Path property will be the path to the Program Files folder.<\/P>\n<P>Now that\u2019s great \u2026 as long as you\u2019re trying to find the Program Files folder on the local computer. If you\u2019re trying to determine the path to the Program Files folder on a remote computer, however, it\u2019s a different story: that\u2019s because the Shell object won\u2019t run against remote machines. On top of that, we couldn\u2019t find a WMI method or property that we can use to retrieve the path. (We thought for <I>sure<\/I> there\u2019d be such a property, but \u2026.) And while there <I>is<\/I> a <I>%programfiles%<\/I> environment variable, we can\u2019t readily retrieve the value of that environment variable from a remote computer either. In other words, we can\u2019t use the Shell object, we can\u2019t use WMI, and we can\u2019t use the <I>%programfiles%<\/I> environment variable. Uh-oh.<\/P>\n<P>All we can say about this is, \u201cThank goodness for the registry.\u201d (We never thought we\u2019d say that, either.) As it turns out, the path to the Program Files folder is stored in the following registry value:<\/P><PRE class=\"codeSample\">HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ProgramFilesDir\n<\/PRE>\n<P>That means that we <I>can<\/I> use WMI to read the registry on a remote computer and return the path:<\/P><PRE class=\"codeSample\">HKEY_LOCAL_MACHINE = &amp;H80000002<\/p>\n<p>strComputer = &#8220;atl-ws-01&#8221;\nSet objReg = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;SOFTWARE\\Microsoft\\Windows\\CurrentVersion&#8221;\nValueName = &#8220;ProgramFilesDir&#8221;<\/p>\n<p>objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue\nWscript.Echo strValue\n<\/PRE>\n<P>So what are we doing with <I>this<\/I> script? Well, we begin by defining a constant named HKEY_LOCAL_MACHINE and setting the value to &amp;H80000002; that tells our script which registry hive to work with. We then use these two lines of code to connect to the WMI service on a remote computer named atl-ws-01:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;\nSet objReg = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)\n<\/PRE>\n<P>Still with us? Good. Next we assign values to two variables, strKeyPath and ValueName:<\/P><PRE class=\"codeSample\">strKeyPath = &#8220;SOFTWARE\\Microsoft\\Windows\\CurrentVersion&#8221;\nValueName = &#8220;ProgramFilesDir&#8221;\n<\/PRE>\n<P>It should come as no surprise that strKeyPath represents the path within the HKLM registry hive, while ValueName represents the name of the registry value we want to read (ProgramFilesDir).<\/P>\n<P>Now we\u2019re ready to call the <B>GetStringValue<\/B> method and read the registry:<\/P><PRE class=\"codeSample\">objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue\n<\/PRE>\n<P>Notice that we pass GetStringValue four parameters: HKEY_LOCAL_MACHINE, strKey, ValueName, and strValue. The first three parameters are a constant and two variables that we defined earlier. But what the heck is strValue?<\/P>\n<P>Well, strValue is an \u201cout parameter.\u201d We don\u2019t specify a value for an out parameter; instead, the method supplies the value. Think of strValue as being a container of sorts; we give GetStringValue the container and GetStringValue then fills that container with the information it reads from the registry. In other words, the answer to our problem &#8211; the path to the Program Files folder &#8211; will be stored in the out parameter strValue.<\/P>\n<P>Which, in turn, means all we have to do now is echo back that value:<\/P><PRE class=\"codeSample\">Wscript.Echo strValue\n<\/PRE>\n<P>And there you have it. It might not have been the most obvious and intuitive way to get the path to the Program Files folder but, hey, any port in a storm, right?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I retrieve the path to the Program Files folder on a computer?&#8212; CC Hey, CC. How can you retrieve the path to the Program Files folder on a computer? Well, to be honest, not as easily as we thought you could. If you\u2019re trying to retrieve the Program Files path [&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,26,3,94,5],"class_list":["post-68273","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-registry","tag-scripting-guy","tag-special-folders","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I retrieve the path to the Program Files folder on a computer?&#8212; CC Hey, CC. How can you retrieve the path to the Program Files folder on a computer? Well, to be honest, not as easily as we thought you could. If you\u2019re trying to retrieve the Program Files path [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68273","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=68273"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68273\/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=68273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}