{"id":69693,"date":"2005-06-01T07:30:00","date_gmt":"2005-06-01T07:30:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/01\/how-can-i-retrieve-the-path-to-the-windir-folder-on-a-computer\/"},"modified":"2005-06-01T07:30:00","modified_gmt":"2005-06-01T07:30:00","slug":"how-can-i-retrieve-the-path-to-the-windir-folder-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-retrieve-the-path-to-the-windir-folder-on-a-computer\/","title":{"rendered":"How Can I Retrieve the Path to the %windir% 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 <I>%windir%<\/I> folder on a computer?<BR><BR>&#8212; DS<\/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, DS. Interesting that you should ask this question. This same question happened to be asked on an internal Microsoft mailing list just the other day, and the answer given (not by one of the Scripting Guys we hasten to add) was an extremely complicated one, an answer involving environment variables, redirected output, the <B>WSHController<\/B> object, human sacrifice, illegal Swiss bank accounts, and ground-up unicorn horn (fresh, not frozen). As near as we could tell, you gather all the above items, mix well, and then call the user and ask them, \u201cHey, could you type <B>echo %windir%<\/B> at the command prompt and tell me what shows up?\u201d<\/P>\n<P>So is it really <I>that<\/I> hard to figure out which folder happens to be the Windows folder? Thankfully, no; all you really need to do is query the WMI class <B>Win32_OperatingSystem<\/B> and echo back the value of the <B>WindowsDirectory<\/B> property:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_OperatingSystem&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.WindowsDirectory\nNext\n<\/PRE>\n<P>As you can see, there\u2019s not much to this script. We begin by connecting to the WMI service on the local computer; if we want to determine the Windows folder on a remote computer, all we have to do is change the value of the variable strComputer to the name of that remote computer. For example, these two lines of code will connect us to the WMI service on the remote computer atl-fs-01:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n<\/PRE>\n<P>After making the connection we use the <B>ExecQuery<\/B> method to retrieve all the instances of the <I>active<\/I> operating system on the computer. Why do we emphasize the word <I>active<\/I>? Well, the Win32_OperatingSystem class returns information only about the operating system currently in use. What if you have a dual-boot machine, one that can boot up in either Windows XP or Windows Server 2003? In that case, Win32_OperatingSystem returns information only about the operating system currently up and running. If you happen to be running Windows XP, you will get back information only about Windows XP; Win32_OperatingSystem will not even acknowledge the presence of the inactive Windows Server 2003 operating system.<\/P>\n<P>Just something to keep in mind.<\/P>\n<P>After that we set up a For Each loop to loop through all the operating systems in the collection (again, there will always be one and only one). Inside that loop we echo the value of the <B>WindowsDirectory<\/B> property, which just happens to be the same thing as the value of %windir%.<\/P>\n<P>By the way, there are at least two other properties of the Win32_OperatingSystem class that might be of interest to you: <B>SystemDrive<\/B>, which returns just the drive letter where the operating system is installed, and <B>SystemDirectory<\/B>, which returns the path to the System folder (typically C:\\Windows\\System32).<\/P>\n<P>Cool, huh? Now if only we\u2019d thought of trying WMI <I>first<\/I>. Anyone interested in several pounds of freshly-ground unicorn horn? Just let us know.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I retrieve the path to the %windir% folder on a computer?&#8212; DS Hey, DS. Interesting that you should ask this question. This same question happened to be asked on an internal Microsoft mailing list just the other day, and the answer given (not by one of the Scripting Guys we [&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,31,3,94,5],"class_list":["post-69693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-operating-system","tag-scripting-guy","tag-special-folders","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I retrieve the path to the %windir% folder on a computer?&#8212; DS Hey, DS. Interesting that you should ask this question. This same question happened to be asked on an internal Microsoft mailing list just the other day, and the answer given (not by one of the Scripting Guys we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69693","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=69693"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69693\/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=69693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}