Skip navigation

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

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 WshShell
Set objWMIService = GetObject("winmgmts:" &_ 
   "\{impersonationLevel=impersonate\}!\\" & strComputer & _
   "\root\cimv2")
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 WshShell
Set objWMIService = GetObject("winmgmts:" & _
   "\{impersonationLevel=impersonate\}!\\" & strComputer & _
   "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from _
   Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
  wscript.echo objOperatingSystem.Caption
Next

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"
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish