Windows – script for installing the service pack for the corresponding windows version

cmd.exescriptservice-packwindows

I'm creating a script for installing the service pack for the corresponding windows version.

So I want the script to determine the Windows version I'm on and the service pack installed and then execute a command to launch the service pack installer

I know I can do wmic os get Caption to get the window version and wmic os get ServicePackMajorVersion to get the service pack.

And I can do start /wait %~dp0\servicepack.exe to launch the service pack, but I don't know how to put these commands together so if I'm on Windows 7 with no service pack it will install the Windows 7 service pack, but if I'm on Windows server it will install the Windows server service pack. Any thoughts?

So I did some research and came up with this:

wmic os get Caption
if /i {Caption}=={Microsoft Windows Server 2008 *} goto :WinS
if /i {Caption}=={Microsoft Windows Vista *} goto :WinV
if /i {Caption}=={Microsoft Windows 7 *} goto :Win7
if /i {Caption}=={Microsoft Windows 8 *} goto :Win8
if /i {Caption}=={Microsoft Windows 8.1 *} goto :Win8.1
echo Windows Version not Suported! 
pause 
exit

:WinS
wmic os get ServicePackMajorVersion
if /i {ServicePackMajorVersion}=={0} start /wait %~dp0\servicepackSV1.exe
if /i {ServicePackMajorVersion}=={1} start /wait %~dp0\servicepackSV2.exe 
if /i {ServicePackMajorVersion}=={2} goto :GoodtoGo 
echo Service Pack Installed!!
goto :
:WinV
wmic os get ServicePackMajorVersion
if /i {ServicePackMajorVersion}=={0} start /wait %~dp0\servicepackSV1.exe
if /i {ServicePackMajorVersion}=={1} start /wait %~dp0\servicepackSV2.exe 
if /i {ServicePackMajorVersion}=={2} goto :GoodtoGo 
echo Service Pack Installed!!
goto :
:Win7
wmic os get ServicePackMajorVersion
if /i {ServicePackMajorVersion}=={0} start /wait %~dp0\servicepack7.exe 
if /i {ServicePackMajorVersion}=={1} goto :GoodtoGo 
echo Service Pack Installed!!
goto :
:Win8
echo No service pack needed!
goto 
:Win8.1
wmic qfe get hotfixid | find "KB2919355"
if %errorlevel%==0 goto :GoodtoGo
start /wait %~dp0\win8.exe
goto :
:GoodtoGo
echo The Latest ServicePack is allready installed! you'r GoodtoGo!
goto

I Have not tested this yet, but will it work?

Best Answer

I don't know how to put these commands together

I know I can do:

   wmic os get Caption

to get the window version and:

   wmic os get ServicePackMajorVersion

to get the service pack.

You can indeed use wmic to retrieve these values. However, in order to use them later in your batch file (for example in an if expression) you need to store the values in an environment variable.

Here is an example of how to do this:

@echo off
setlocal
setlocal enabledelayedexpansion
set _os=
set _sp=
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=3" %%i in (`wmic os get caption ^| findstr /r /v "^$"`) do (
  set "_os=%%i"
  )
for /f "usebackq skip=1 tokens=*" %%i in (`wmic os get ServicePackMajorVersion ^| findstr /r /v "^$"`) do (
  set "_sp=%%i"
  )
echo Operating System: %_os%
echo Service Pack Major Version: %_sp%
endlocal

Now we have the OS stored in %_os and the Service Pack Major Version stored in %_sp%.

Notes:

  1. We can't use regular expressions in a string comparison, they are not supported.

  2. The for command retrieves only the 3rd part (token) of the OS. This will work for the desktop versions (if you want to distinguish Server 2008 from other versions you will need to find another solution).

  3. _os will be set to one of the following values Server, Vista, 7, 8, or 8.1, so a regular expression is no longer needed.

Next we need to change the if statements so they use the stored variables and correctly perform the string comparisons.

if /i {Caption}=={Microsoft Windows Server 2008 *} goto :WinS

becomes:

if /i "%_os%" == "Server" goto :WinS

etc.

Putting it all together, use the following batch file:

@echo off
setlocal
setlocal enabledelayedexpansion
set _os=
set _sp=
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=3" %%i in (`wmic os get caption ^| findstr /r /v "^$"`) do (
  set "_os=%%i"
  )
for /f "usebackq skip=1 tokens=1" %%i in (`wmic os get ServicePackMajorVersion ^| findstr /r /v "^$"`) do (
  set "_sp=%%i"
  )
echo Operating System: %_os%
echo Service Pack Major Version: %_sp%

if /i "%_os%" == "7" echo Win7

if /i "%_os%" == "Server" goto :WinS
if /i "%_os%" == "Vista"  goto :WinV
if /i "%_os%" == "7"      goto :Win7
if /i "%_os%" == "8"      goto :Win8
if /i "%_os%" == "8.1"    goto :Win8.1
echo Windows Version not Supported! 
pause 
goto :eof

:WinS
if /i "%_sp%" == "0" start /wait %~dp0\servicepackSV1.exe
if /i "%_sp%" == "1" start /wait %~dp0\servicepackSV2.exe 
if /i "%_sp%" == "2" goto :GoodtoGo 
echo Service Pack Installed!!
goto :eof

:WinV
if /i "%_sp%" == "0" start /wait %~dp0\servicepackSV1.exe
if /i "%_sp%" == "1" start /wait %~dp0\servicepackSV2.exe 
if /i "%_sp%" == "2" goto :GoodtoGo 
echo Service Pack Installed!!
goto :eof

:Win7
if /i "%_sp%" == "0" start /wait %~dp0\servicepack7.exe 
if /i "%_sp%" == "1" goto :GoodtoGo 
echo Service Pack Installed!!
goto :eof

:Win8
echo No service pack needed!
goto :eof

:Win8.1
wmic qfe get hotfixid | find "KB2919355"
if %errorlevel%==0 goto :GoodtoGo
start /wait %~dp0\win8.exe
goto :eof

:GoodtoGo
echo The Latest ServicePack is already installed!
goto :eof

endlocal

Notes:

  • Not completely tested as I don't have all the OS and SP combinations to test it with.
  • Some broken goto statements were fixed as well.

What about 32 and 64 bit. How can I determine what system I'm?

Use the following batch file (GetBits.cmd):

@echo off
setlocal enabledelayedexpansion
for /f "usebackq skip=1 tokens=*" %%i in (`wmic OS get OSArchitecture ^| findstr /r /v "^$"`) do (
  set "_bits=%%i"
  )
echo %_bits%
endlocal

Example output:

F:\test>GetBits
64-bit

F:\test>

Further Reading

Related Question