midiboy

Check for OS Version in a script

18 posts in this topic

Hi !

Is it possible to check for the installed Windows version in a small cmd script ? I would like to check if the OS is either Windows Server 2003, Windows XP, Windows 2000 or Windows Vista.

However the variable "OS" is returning Windows_NT in all 3 cases. Any ideas ?

Thanks for your help !

Alex

0

Share this post


Link to post
Share on other sites

Try this VBS script

Save As OsName.vbs

strComputer = "." 
Set Wmi = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = Wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48)
For Each objItem in colItems
msgbox "Os Name: " & objItem.Caption & " - " & objItem.Version,4128,"Os Name"
Next

Edited by gunsmokingman
0

Share this post


Link to post
Share on other sites

Hi gunsmokingman !

You are great (as usual!) :hello:

Thanks !

Just two more questions ...

1. since I am not good in vbs ... how can I add the OS Name into a variable from inside the vbs and use that variable in a cmd ? (I would start the vbs from inside that cmd)

2. Would it be possible to just strip the output to XP, Vista, 2003 or 2000 for the 4 major OSes and not have all the subversions (like Home, Standard, build numbers etc.) in the name ? Thats because I would simply like to do different stuff for Vista than for XP/2000/2003 so I just need to know if the OS is Vista or XP or 2000/2003.

Thanks for your help !

Alex

0

Share this post


Link to post
Share on other sites

Will this do what you want?

@echo off&setlocal
for /f "delims=" %%? in ('net config work^|findstr/b Soft') do call :OSis %%?
echo/%%WinOS%% is %WinOS%
endlocal&goto :eof
:OSis
echo/%*|find "2000">nul 2>&1&&(set "WinOS=2000"&goto :eof)
echo/%*|find "2002">nul 2>&1&&(set "WinOS=XP"&goto :eof)
echo/%*|find "2003">nul 2>&1&&(set "WinOS=2003"&goto :eof)
echo/%*|find "Vista">nul 2>&1&&(set "WinOS=Vista"&goto :eof)

0

Share this post


Link to post
Share on other sites

Here is the VBS script inside of the cmd it passes the OS Name back to the

original cmd.

Save as OsName.cmd

@Echo Off
CLS
Color F3
Mode 55,5
Title Time
MkDir %SystemDrive%\OsTemp
Set VBS=%SystemDrive%\OsTemp\OsName.vbs
Set Cmd=%SystemDrive%\OsTemp\Name.cmd
> %VBS% Echo Dim TS
>> %VBS% Echo strComputer = "."
>> %VBS% Echo Set Wmi = GetObject("winmgmts:\\" ^& strComputer ^& "\root\CIMV2")
>> %VBS% Echo Set colItems = Wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48)
>> %VBS% Echo Dim Name
>> %VBS% Echo For Each objItem in colItems :Name=objItem.Caption :Next
>> %VBS% Echo If InStr(Name,"Vista") Then Name = "Vista"
>> %VBS% Echo If InStr(Name,"2003") Then Name="Server 2003"
>> %VBS% Echo If InStr(Name,"XP") Then Name="XP"
>> %VBS% Echo If InStr(Name,"2000") Then Name="Windows 2000"
>> %VBS% Echo Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
>> %VBS% Echo Set Ts = Fso.CreateTextFile("%Cmd%")
>> %VBS% Echo Ts.writeline "Set Name=" ^& Name '/-^> Varible For Name
>> %VBS% Echo Ts.Close
%VBS%
call %Cmd%
Del %VBS%
Del %Cmd%
RmDir %SystemDrive%\OsTemp
Echo.
Echo -^> %Name%
Echo.
pause

Edited by gunsmokingman
0

Share this post


Link to post
Share on other sites

You are good and so very helpful !

Thanks a lot, gunsmokingman !

PS: The first cmd has a little error:

endlocal&goto :eof should be

endlocal&goto :OSis

Thanks so much !

Bye,

Alex

Edited by Yzöwl
blasphemous remark removed!
0

Share this post


Link to post
Share on other sites
PS: The first cmd has a little error:

endlocal&goto :eof should be

endlocal&goto :OSis

There's only one person who's made a little error there, and it's certainly not me!

If you can guarantee that the command interpreter, (command.com / cmd.exe), is the original one for the said OS then use ther built in ver command.

@echo off
ver | find "95">nul
if not errorlevel 1 (
set WinOS=95
goto OSis)
ver | find "98">nul
if not errorlevel 1 (
set WinOS=98
goto OSis)
ver | find "Millennium">nul
if not errorlevel 1 (
set WinOS=ME
goto OSis)
ver | find "NT">nul
if not errorlevel 1 (
set WinOS=NT
goto OSis)
ver | find "2000">nul
if not errorlevel 1 (
set WinOS=2000
goto OSis)
ver | find "XP">nul
if not errorlevel 1 (
set WinOS=XP
goto OSis)
ver | find "2003">nul
if not errorlevel 1 (
set WinOS=2003
goto OSis)
ver | find "Vista">nul
if not errorlevel 1 (
set WinOS=Vista)
:OSis
echo.Your OS is %WinOS%

0

Share this post


Link to post
Share on other sites

HI !

There's only one person who's made a little error there, and it's certainly not me!

I did not mean any disrespect ! :unsure:

The script just did not work here, after I changed

endlocal&goto :eof

to

endlocal&goto :OSis

it did.

But anyway, you gave me lots of options to solve the problem so Thanks again ! :hello:

0

Share this post


Link to post
Share on other sites

I didn't see it a disrespectful...just incorrect!

The last line of the batch file is

endlocal&goto :eof

The piece of code within/below the OSis label has already been run from the call to it earlier within the 'for in do' command.

0

Share this post


Link to post
Share on other sites
The piece of code within/below the OSis label has already been run from the call to it earlier within the 'for in do' command.

Maybe the problem relies in a nationalized version of the OS midiboy is running.

Your small batch does not work on my system (italian) because of the "findstr/b Soft", changing it to "findstr/i Soft" works, as instead of "Software version" the output of the command contains "Versione del software", and thus the CALL to :OSis is never made.

jaclaz

0

Share this post


Link to post
Share on other sites

Thanks for the information, jaclaz.

It's sometimes difficult to remember that although the forum is English speaking, our OSes aren't necessarily so.

(especially when the originator appears to use perfect English and doesn't state their origin/language in their profile!)

@echo off&setlocal
for /f "delims=" %%? in ('net config work^|findstr/i "\<soft"') do call :OSis %%?
echo/%%WinOS%% is %WinOS%
endlocal&goto :eof
:OSis
echo/%*|find "2000">nul 2>&1&&(set "WinOS=2000"&goto :eof)
echo/%*|find "2002">nul 2>&1&&(set "WinOS=XP"&goto :eof)
echo/%*|find "2003">nul 2>&1&&(set "WinOS=2003"&goto :eof)
echo/%*|find "Vista">nul 2>&1&&(set "WinOS=Vista"&goto :eof)

0

Share this post


Link to post
Share on other sites

Just as a confirmation:

findstr/i "\<soft"

works in Italian. :thumbup

jaclaz

0

Share this post


Link to post
Share on other sites

Hi gunsmokingman !

(especially when the originator appears to use perfect English and doesn't state their origin/language in their profile!)

Thanks for the compliment (spent some time in the USA once :whistle: ) and I have updated the profile !

And sorry for drawing the wrong conclusions!

Have a nice day !

Alex

0

Share this post


Link to post
Share on other sites

Hi again,

here´s a followup question: :blink:

Is there a way to test if the OS is 32 or 64 bit too ? At least with the net config command it does not seem to be possible ?

Thanks again !

Alex

0

Share this post


Link to post
Share on other sites

If it was for Vista then it would easy. Windows Server 2003, Windows 2000, Windows NT 4.0, Windows XP, and Windows Me/98/95. This property is not available.

Example for Vista only

strComputer = "." 
Set Wmi = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = Wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48)
For Each objItem in colItems
msgbox "OS Architecture: " & objItem.OSArchitecture,4128,"OS Architecture"
Next

There is a possible way of seeing if it running in 32 or 64 using the Processor class.

This displayed that my OS is running the CPU in 32 bit and shows I have a 64 bit cpu.

StrComputer="."
Set Wmi = GetObject("winmgmts:\\" & StrComputer & "\root\CIMV2")
Set colItems = Wmi.ExecQuery("SELECT * FROM Win32_Processor",,48)
Dim CpuArchitecture
For Each objItem in colItems
If objItem.Architecture = 0 Then CpuArchitecture = "x86"
If objItem.Architecture = 1 Then CpuArchitecture = "MIPS"
If objItem.Architecture = 2 Then CpuArchitecture = "Alpha"
If objItem.Architecture = 3 Then CpuArchitecture = "PowerPC"
If objItem.Architecture = 6 Then CpuArchitecture = "Intel Itanium Processor Family (IPF)"
If objItem.Architecture = 9 Then CpuArchitecture = "x64"
MsgBox "Address Width " & vbTab & objItem.AddressWidth & vbCrLf &_
"Cpu Architecture" & vbTab & CpuArchitecture,4128,"OS Architecture"
Next

0

Share this post


Link to post
Share on other sites

Hi gunsmokingman,

again, thanks a lot ! How come you know all those commands ? Is there a magic book where you look these things up or do you simply know them ? :thumbup

Bye and have a nice day !

Alex

0

Share this post


Link to post
Share on other sites

It's simply Visual Basic. Learn it ;)

0

Share this post


Link to post
Share on other sites

Hi Yzöwl,

I have been using your code successfully for months now:

for /f "delims=" %%? in ('net config work^|findstr/i "\<soft"') do call :OSis %%?

if %WinOS% == Vista goto :Vista
if %WinOS% == XP goto :XP

:OSis
echo/%*|find "2000">nul 2>&1&&(set "WinOS=XP"&goto :eof)
echo/%*|find "2002">nul 2>&1&&(set "WinOS=XP"&goto :eof)
echo/%*|find "2003">nul 2>&1&&(set "WinOS=XP"&goto :eof)
echo/%*|find "Vista">nul 2>&1&&(set "WinOS=Vista"&goto :eof)
echo/%*|find "2008">nul 2>&1&&(set "WinOS=Vista"&goto :eof)

Thanks again for it ! :-)

However, recently I am having a problem with this code on WinXP SP3 during runonceex. Seemingly out of nowhere the cmd stops right after this command:

for /f "delims=" %%? in ('net config work^|findstr/i "\<soft"') do call :OSis %%?

This does not happen on all the scripts where this code is present, just one. The strange thing is, if I press the ENTER key, it continues to run normally, no error appears, nothing. But it just stops there forever if noone presses a key.

Do you maybe have an explanation for it or a way around it ?

Thanks in advance,

Alex

Edited by midiboy
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.