Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I retrieve the path to the %windir% folder on a computer?

-- DS

SpacerHey, Scripting Guy! AnswerScript Center

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 WSHController 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, “Hey, could you type echo %windir% at the command prompt and tell me what shows up?”

So is it really that 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 Win32_OperatingSystem and echo back the value of the WindowsDirectory property:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")

For Each objItem in colItems
    Wscript.Echo objItem.WindowsDirectory
Next

As you can see, there’s 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:

strComputer = "atl-fs-01"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

After making the connection we use the ExecQuery method to retrieve all the instances of the active operating system on the computer. Why do we emphasize the word active? 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.

Just something to keep in mind.

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 WindowsDirectory property, which just happens to be the same thing as the value of %windir%.

By the way, there are at least two other properties of the Win32_OperatingSystem class that might be of interest to you: SystemDrive, which returns just the drive letter where the operating system is installed, and SystemDirectory, which returns the path to the System folder (typically C:\Windows\System32).

Cool, huh? Now if only we’d thought of trying WMI first. Anyone interested in several pounds of freshly-ground unicorn horn? Just let us know.