How Can I Determine Which Version of Windows Vista is Installed on a Computer?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine which version of Windows Vista is installed on a computer?

— GC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, GC. You know, Microsoft is one of the largest and most successful companies the world has ever known. And yet, despite all our successes, we’re really no different than any anyone else: for example, from time-to-time we get a little jealous of other companies and what they have achieved. But that’s understandable after all, for years we Microsoft employees have had to endure the indignity of driving past Baskin-Robbins ice cream stores and seeing the sign advertising their 31 different flavors of ice cream. Thirty-one different flavors! How in the world can we compete with that?

But, then again, we are Microsoft. You say that Baskin-Robbins has 31 different flavors of ice cream? Good for them; just wait and see how many different flavors of Windows Vista we can release. Let’s see, there’s Windows Vista Home Basic and Windows Vista Home Premium. There’s Windows Vista Enterprise edition, Windows Vista Ultimate, Windows Vista White Chocolate and Macadamia Nuts. There’s – well, you get the idea. Thanks to Windows Vista, all us Microsoft employees can now drive past Baskin-Robbins with our heads held high.

Note. OK, technically some of us don’t actually drive past a Baskin-Robbins; instead, we pull in and order two or three scoops of Chocolate Fudge ice cream. But you know what we mean.

As a general rules, more choices are better than fewer choices, on the other hand, it’s also true that having 31 flavors of Windows Vista can complicate life for system administrators. This is especially true in light of the fact that these different flavors of Vista have different capabilities. (For example, do you want BitLocker drive encryption? Then you need to install Windows Vista Ultimate.) But how in the world can you determine whether or not a computer is even running Windows Vista, let alone which flavor of Vista that computer is running?

Funny you should ask:

strComputer = “.”

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

Set colItems = objWMIService.ExecQuery(“Select * From Win32_OperatingSystem”)

For Each objItem in colItems If Instr(objItem.Caption, “Vista”) Then Select Case objItem.OperatingSystemSKU Case 0 strVersion = “Undefined.” Case 1 strVersion = “Ultimate Edition.” Case 2 strVersion = “Home Basic Edition.” Case 3 strVersion = “Home Basic Premium Edition.” Case 4 strVersion = “Enterprise Edition.” Case 5 strVersion = “Home Basic N Edition.” Case 6 strVersion = “Business Edition.” Case 7 strVersion = “Standard Server Edition.” Case 8 strVersion = “Datacenter Server Edition.” Case 9 strVersion = “Small Business Server Edition.” Case 10 strVersion = “Enterprise Server Edition.” Case 11 strVersion = “Starter Edition.” Case 12 strVersion = “Datacenter Server Core Edition.” Case 13 strVersion = “Standard Server Core Edition.” Case 14 strVersion = “Enterprise Server Core Edition.” Case 15 strVersion = “Enterprise Server IA64 Edition.” Case 16 strVersion = “Business N Edition.” Case 17 strVersion = “Web Server Edition.” Case 18 strVersion = “Cluster Server Edition.” Case 19 strVersion = “Home Server Edition.” Case 20 strVersion = “Storage Express Server Edition.” Case 21 strVersion = “Storage Standard Server Edition.” Case 22 strVersion = “Storage Workgroup Server Edition.” Case 23 strVersion = “Storage Enterprise Server Edition.” Case 24 strVersion = “Server For Small Business Edition.” Case 25 strVersion = “Small Business Server Premium Edition.” End Select Wscript.Echo “This computer is running Windows Vista ” & strVersion Else Wscript.Echo “This computer is not running Windows Vista.” End If Next

What’s that? Can we explain how this script work? You know what? Not only can we explain how this script, we will explain how this script works.

Note. Which, we might add, puts us yet another step ahead of Baskin-Robbins. How often do the folks at Baskin-Robbins offer to tell you how they make their Old-Fashioned Butter Pecan Ice Cream?

The script starts out by connecting to the WMI service on the local computer. Can we run this same script against a remote computer? Of course we can; after all, the script wouldn’t be particularly useful if you couldn’t. To run this script against a remote computer (and thus determine the flavor of Vista running on that remote machine), simply assign the name of the remote computer to the variable strComputer:

strComputer = “atl-ws-01”

Note. Yes, you can also run this script against an entire flock of computers (e.g., all the computers in an OU or all the computers listed in a text file). For sample code take a peek at our Multiple/Remote Computer Scripting Templates.

After we connect to the WMI service we then call the ExecQuery method and return a collection of all the operating systems currently in use on the computer:

Set colItems = objWMIService.ExecQuery(“Select * From Win32_OperatingSystem”)

Admittedly, there will only be one item in the collection; that’s because only one operating system can be in use at any given time. However, because this is a collection we still need to set up a For Each loop to loop through each and every operating system in the collection:

For Each objItem in colItems 

And what are we going to do inside that loop? Well, the first thing we’re going to do is determine whether or not our computer is running any flavor of Windows Vista. That’s what this line of code is for:

If Instr(objItem.Caption, “Vista”) Then

All we’re doing here is looking at the value of the operating system’s Caption property and checking to see if the word Vista appears anywhere within that caption. (That’s what the InStr function does; it checks to see if a given substring can be found anywhere inside a string value.) If InStr returns a 0 that means that the word the Vista couldn’t be found anywhere in the Caption. In that case, we assume that the computer is not running Windows Vista, and we echo back the appropriate message:

Wscript.Echo “This computer is not running Windows Vista.”

But suppose InStr returns a value other than 0; for example, suppose our operating system caption is this:

Microsoft Windows Vista Enterprise

In that case, that means that the computer is running Windows Vista. And, in turn, that means we need to try and determine which version of Vista we’re dealing with.

But don’t worry: we don’t have to write some code that tries to tease the word Enterprise out of the Caption property. Instead, we use a brand-new property added to the Win32_OperatingSystem class: OperatingSystemSKU. The OperatingSystemSKU property returns an integer value that identifies the “SKU” (stock-keeping unit) for the flavor of Vista running on the computer. Admittedly, we could just report back one of these integer values (e.g., a 4 for a computer running Windows Vista Enterprise edition); that’s probably what Baskin-Robbins would do. But Microsoft is not Baskin-Robbins.

Note. Do the Scripting Guys wish Microsoft was Baskin-Robbins? We probably shouldn’t answer that question ….

Because we are Microsoft, however, we’ve taken the extra step and set up a Select Case statement that determines the value of the OperatingSystemSKU property and then assigns the corresponding operating system flavor to a variable named strVersion. For example, if the OperatingSystemSKU really does return the value 4, then the following line of code is executed (and the value gets assigned to the variable strVersion):

Case 4 strVersion = “Enterprise Edition.” 

After we dispose of the Select Case block all that’s to do is run the following line of code:

Wscript.Echo “This computer is running Windows Vista ” & strVersion

In turn, that’s going to cause a message similar to this one to appear onscreen:

This computer is running Windows Vista Enterprise Edition.

And there you have it.

Of course, we realize that many of you might be a little worried about Microsoft; after all, having successfully matched Baskin-Robbins flavor-for-flavor, well, maybe we’ll get a little complacent, maybe we’ll just sit back and rest on our laurels. Take it from the Scripting Guys: you have nothing to worry about. True, we now have as many flavors of Vista as Baskin-Robbins does ice cream. On the other hand, Baskin-Robbins still holds the world record for the largest Ice Cream Scoop Pyramid (achieved on May 18, 2000 using 3,100 scoops of ice cream). Does that mean that Microsoft is busy working on a Windows Vista Ice Cream Scoop Pyramid? Well, we’re not allowed to discuss unreleased and unannounced products. At this point in time all we can say is this: at Microsoft, we never back away from a challenge.



Comments are closed. Login to edit/delete your existing comments