• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4099
  • Last Modified:

Simple: detect OS and SP


I need a batch file to detect what is the OS and Service Pack the pc has on and save the OS in the variable %os and the SP in the variable %sp

  • 2
3 Solutions
Here is a VBScript that will do what you want... it requires the SETX.EXE command (from either the Resource Kit or Support Tools).  You might have to place the SETX.EXE on a network share, since you probably won't be certain if it exists on every client PC.

dim obj, objs, wsh
dim OperatingSystem, ServicePack

Set objs = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
for each obj in objs
    OperatingSystem = obj.Caption
    ServicePack = obj.CSDVersion

Set wsh = CreateObject("WScript.Shell")
wsh.run "setx OS " & chr(34) & OperatingSystem & chr(34) & " /m", 7, false
wsh.run "setx SP " & chr(34) & ServicePack & chr(34) & " /m", 7, false
Lee W, MVPTechnology and Business Process AdvisorCommented:
The following command relies only on DOS CMD extensions and the utility PSINFO, available free in the PSTOOLS package from SysInternals:

@Echo off
FOR /f "usebackq tokens=2 delims=:" %%a in (`psinfo ^| find /i "Product version"`) do SET ProdVer=%%a
FOR /f "usebackq tokens=2 delims=:" %%a in (`psinfo ^| find /i "Service pack"`) do SET Prodsp=%%a
Echo Windows %Prodver%, Service Pack %prodsp%

The above script sets Prodver and prodsp environment variables to the version of Windows and the service pack respectively.
One problem the Variable %os% cannot be Overwritten as it is a fixed variable. therefor i will have to use %os1%
This is how you do it:~

----Start of Batch Script!----
@echo off
systeminfo | find /n "OS" > temp.txt
for /f "tokens=2* delims=:" %%a in ('type temp.txt ^| find "[4]"') do set sp=%%a
for /f "tokens=2* delims=:" %%a in ('type temp.txt ^| find "[3]"') do set os1=%%a
del temp.txt
set os1=%os1:  =%
set sp=%sp:  =%
:: The variables %sp% and %os% are Now set.
-----End of Batch Script!----

Good Luck!
- Shane
Lee W, MVPTechnology and Business Process AdvisorCommented:
I feel all three comments answer the question, though ShaneKelly has a valid point about the %OS% variable and graye's comment did attempt to reset that.  

I believe my solution represents the best answer where the OS is NT, 2000, or XP.  I do not believe systeminfo functions in NT.  And the VBScript solution is both longer and requires the computer have VSH installed and running (NT 4 might be an issue with this).  

Regardless, I think the points should be split evenly amongst the three of us.

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now