How can I use a script to determine a machine's OS version?

John Savill

June 15, 2005

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. You can use Windows Management Instrumentation (WMI) to query the OperatingSystem information on the local machine. To do so, run the following script, which you can download at http://www.windowsitpro.com/content/content/46759/OSinfo.zip

strComputer = "."Set WshShell = WScript.CreateObject("WScript.Shell")Dim WshShellSet objWMIService = GetObject("winmgmts:" &_    "{impersonationLevel=impersonate}!\" & strComputer & _   "rootcimv2")Set colOperatingSystems = objWMIService.ExecQuery ("Select * from _   Win32_OperatingSystem")For Each objOperatingSystem in colOperatingSystems  if objOperatingSystem.Caption = "Microsoft Windows 2000_   Professional"then wscript.echo "2000"  if objOperatingSystem.Caption = "Microsoft Windows XP _   Professional"then wscript.echo "XP"Next

You can modify the script to perform various other actions and add other OSs by adding more "if" clauses. You can determine the name of other OSs by running the following script, which you can download at http://www.windowsitpro.com/content/content/46759/OtherOS.zip, on the local machine:

strComputer = "."Set WshShell = WScript.CreateObject("WScript.Shell")Dim WshShellSet objWMIService = GetObject("winmgmts:" & _   "{impersonationLevel=impersonate}!\" & strComputer & _   "rootcimv2")Set colOperatingSystems = objWMIService.ExecQuery ("Select * from _   Win32_OperatingSystem")For Each objOperatingSystem in colOperatingSystems  wscript.echo objOperatingSystem.CaptionNext

If you run the script on a machine that uses Windows Server 2003, Standard Edition, for example, you'll see output similar to that which the figure shows. Thus, to determine whether the machine is running Windows 2003 Standard, you add the following line to the OS check portion of the main script:

if objOperatingSystem.Caption = "Microsoft(R) Windows(R) _   Server 2003, Standard Edition "then wscript.echo "2003 Std"

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like