VBScript to get OS version and use it in teh script logic

I have found several examples of this on the internet, but I'm not sure what is the best way to accomplish what I need.  I want to be able to determine if the OS of a computer is XP or Windows 7 and then run logic based on the returned OS value.  What is the best value to use and could someone give me the code that retrieves that value from the computer?
TPBPITAsked:
Who is Participating?
 
Bill PrewCommented:
Doing this in VBS, you can do something like this:

For Each objOS in GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem")
   strOsCaption = objOS.Caption
   strOsVersion = objOS.Version
Next
If Instr(strOsCaption, "Vista") > 0 Or Instr(strOsCaption, "Windows 7") > 0 Then
   strWinVer = "Win7"
Else
   strWinVer = "XP"
End If
Wscript.Echo strWinVer

Open in new window

~bp
0
 
redbmasterCommented:
The best way I have found to do this was with a bat file. Here is the code I received:

ver | find "2003" > nul
if %ERRORLEVEL% == 0 goto ver_2003

ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp

ver | find "2000" > nul
if %ERRORLEVEL% == 0 goto ver_2000

ver | find "NT" > nul
if %ERRORLEVEL% == 0 goto ver_nt

if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit

systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i

echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7

echo %vers% | find "Windows Server 2008" > nul
if %ERRORLEVEL% == 0 goto ver_2008

echo %vers% | find "Windows Vista" > nul
if %ERRORLEVEL% == 0 goto ver_vista

goto warnthenexit

:ver_7
:Run Windows 7 specific commands here.
echo Windows 7
goto exit

:ver_2008
:Run Windows Server 2008 specific commands here.
echo Windows Server 2008
goto exit

:ver_vista
:Run Windows Vista specific commands here.
echo Windows Vista
goto exit

:ver_2003
:Run Windows Server 2003 specific commands here.
echo Windows Server 2003
goto exit

:ver_xp
:Run Windows XP specific commands here.
echo Windows XP
goto exit

:ver_2000
:Run Windows 2000 specific commands here.
echo Windows 2000
goto exit

:ver_nt
:Run Windows NT specific commands here.
echo Windows NT
goto exit

:warnthenexit
echo Machine undetermined.

:exit

Open in new window

0
 
redbmasterCommented:
Seeing as how you only care to differentiate between Win XP and 7 then you dont really need the second half of the script. The reason that is there is to differentiate between Win 7 and Server 08. Here is actually what I am using in my scripts:

ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp

ver | find "Vista" > nul
if %ERRORLEVEL% == 0 goto ver_vista

ver | find "7" > nul
if %ERRORLEVEL% == 0 goto ex

Open in new window

it
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
TPBPITAuthor Commented:
Yeah, I already have a pretty lengthy vbs script writen for XP and ust need to modify a few sections for changes in the directory structure on WIndows 7.  I need to kept it in VBS.
0
 
TPBPITAuthor Commented:
That's what I needed.  Thanks.
0
 
Bill PrewCommented:
Welcome.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.