<http://www.netikka.net/tsneti/info/tscmd145.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Sat 17-Apr-2010 15:15:36

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



145} How to find out if WinXP SP2 (or SP3...) is installed via a script?

With systeminfo:
  @echo off & setlocal enableextensions
  systeminfo | findstr /b "OS.*Pack.2" && echo SP2 installed
  endlocal & goto :EOF

The output would be something like
  C:\_D\TEST>cmdfaq
  OS Version:                5.1.2600 Service Pack 2 Build 2600
  SP2 installed

Or, more generally
  @echo off & setlocal enableextensions
  systeminfo|findstr /b "OS.Version.*Pack"
  for /f "tokens=4-6" %%a in ('
    systeminfo^|findstr /b "OS.Version.*Pack"') do (
      set spver_=%%a %%b %%c)
  echo spver_=%spver_%
  endlocal & goto :EOF

The output would be something like
  C:\_D\TEST>cmdfaq
  OS Version:                5.1.2600 Service Pack 2 Build 2600
  spver_=Service Pack 2
or
  C:\_D\TEST>cmdfaq
  OS Version:                5.1.2600 Service Pack 3 Build 2600
  spver_=Service Pack 3

The information can also be obtained from the registry
  @echo off & setlocal enableextensions
  ::
  :: Get the Operating System

  for /f "tokens=2,*" %%i in (
  'reg query "HKLM\software\microsoft\windows nt\currentversion" /v ProductName^
    ^| find "REG_SZ"') do set prod_=%%j
  echo.%prod_%
  ::
  :: Get the service pack version

  for /f "tokens=2,*" %%i in (
  'reg query "HKLM\software\microsoft\windows nt\currentversion" /v CSDVersion^
    ^| find "REG_SZ"') do (
    set pack_=%%j)
  echo.%pack_%
  endlocal & goto :EOF

The output would be something like
  C:\_D\TEST>cmdfaq
  Microsoft Windows XP
  Service Pack 2

Alternatively
  @echo off & setlocal enableextensions
  ::
  :: Get the Operating System

  for /f "tokens=2,*" %%i in (
  'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName^
    ^| find "REG_SZ"') do set prod_=%%j
  echo.%prod_%
  ::
  :: Get the service pack version

  for /f "tokens=2,*" %%i in (
    'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CSDVersion^
    ^| find "REG_SZ"') do (
    set pack_=%%j)
  echo.%pack_%
  endlocal & goto :EOF

Another method, in XP Professional and beyond is to use WMIC (Windows Management Instrumentation Command-Line Utility). This, however, is a bit more drastic requiring administrator status.
  @echo off & setlocal enableextensions
  for /f "tokens=* skip=1" %%i in ('wmic OS Get CSDVersion') do (
    set pack_=%%i)
  echo.%pack_%
  endlocal & goto :EOF

The output would be something like
  C:\_D\TEST>cmdfaq
  Service Pack 2

Or
  @echo off & setlocal enableextensions
  for /f "tokens=* skip=1" %%i in ('wmic OS Get ServicePackMajorVersion') do (
    set packMajor_=%%i)
  for /f "tokens=* skip=1" %%i in ('wmic OS Get ServicePackMinorVersion') do (
    set packMinor_=%%i)
  echo.%packMajor_: =%.%packMinor_: =%
  endlocal & goto :EOF

The output would be something like
  C:\_D\TEST>cmdfaq
  2.0

Another, VBS-aided solution:
  @echo off & setlocal enableextensions
  ::
  :: Build a Visual Basic Script and run it

  set vbs_=%temp%\tmp$$$.vbs
  set skip=
  findstr "'%skip%VBS" "%~f0" > "%vbs_%"
  for /f %%a in ('cscript //nologo "%vbs_%"') do set pack_=%%a
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  ::
  echo pack_=%pack_%
  ::
  endlocal & goto :EOF
  '
  'The Visual Basic Script

  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 'VBS
  Set colOSes = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem") 'VBS
  For Each objOS in colOSes 'VBS
    WScript.Echo objOS.ServicePackMajorVersion & "." & objOS.ServicePackMinorVersion 'VBS
  Next 'VBS

The output would be something like
  C:\_D\TEST>cmdfaq
  pack_=2.0

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Aug 6 2006, 9:32 pm [M]
  WMIC
  List Operating System and Service Pack Information Microsoft TechNet Script Repository