Downloads
44016.zip

I have some VBScript files that need to run on different OSs. How can I tell which OS my script is running on?

You can use Windows Management Instrumentation's (WMI's) Win32_Operating System class to discover OS information. Like its name suggests, this class represents the host computer's OS.

GetOSVersion.vbs, which Listing 1 shows, demonstrates how to use the Win32_Operating System class. At the beginning of the script, you use the Option Explicit statement to ensure that all local variables are declared. You then use Dim statements to declare those variables. Although including the Option Explicit and Dim statements might add a few extra lines to your script, it can save you hours of debugging time.

Next, you set the oWMIService variable to an instance of WMI's root object, then use that object's ExecQuery method to retrieve a collection of all the properties in the Win32_OperatingSystem class. You use a For Each...Next statement to iterate through the collection. Within the For Each loop, you retrieve the computer name, OS name, OS version, and service pack version. In the case of the OS name, the value contains both the name and a path value separated by the pipe (|) character. Thus, you need to use the InStr and Left functions to extract just the OS name portion of the value. Finally, you use Windows Script Host's (WSH's) Echo method to display the information you just retrieved.