Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 397 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get Windows version in a batch file

#1
I need to get the OS version with a batch file. I 've seen a lot of examples online, many uses something like this code:

@echo off

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

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 Vista" > nul
if %ERRORLEVEL% == 0 goto ver_vista

goto warnthenexit

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

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

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

:warnthenexit
echo Machine undetermined.

:exit

The problem is when I execute this on Vista or Windows 7 I get the message

> Machine undetermined

Is there any other way to do what I want?
Reply

#2
Here's my one liner to determine the windows version:

for /f "tokens=1-9" %%a in ('"systeminfo | find /i "OS Name""') do (set ver=%%e & echo %ver%)

This returns the windows version, *i.e.*, XP, Vista, 7, and sets the value of "ver" to equal the same...
Reply

#3
Have you tried using the `wmic` commands?

Try
`wmic os get version`

This will give you the version number in a command line, then you just need to integrate into the batch file.
Reply

#4
Actually, that one liner doesn't work for windows xp since the ver contains xp in the string. Instead of 5.1 which you want, you would get [Version.5 because of the added token.

I modified the command to look like this:
`for /f "tokens=4-6 delims=[. " %%i in ('ver') do set VERSION=%%i.%%j`

This will output Version.5 for xp systems which you can use to indentify said system inside a batch file. Sadly, this means the command cannot differentiate between `32bit` and `64bit` build since it doesn't read the .2 from 5.2 that denotes `64bit` XP.

You could make it assign `%%k` that token but doing so would make this script not detect windows vista, 7, or 8 properly as they have one token less in their ver string. Hope this helps!
Reply

#5
@echo off
for /f "tokens=2 delims=:" %%a in ('systeminfo ^| find "OS Name"') do set OS_Name=%%a
for /f "tokens=* delims= " %%a in ("%OS_Name%") do set OS_Name=%%a
for /f "tokens=3 delims= " %%a in ("%OS_Name%") do set OS_Name=%%a
if "%os_name%"=="XP" set version=XP
if "%os_name%"=="7" set version=7
This will grab the OS name as "7" or "XP"

Then you can use this in a variable to do certain commands based on the version of windows.
Reply

#6
This will return 5.1 for xp or 6/1 for windows 7

for /f "tokens=4-7 delims=[.] " %%i in ('ver') do (
if %%i == Version set OSVersion=%%j.%%k
if %%i neq Version set OSVersion=%%i.%%j
)
Reply

#7
I know it's an old question but I thought these were useful enough to put here for people searching.

This first one is a simple batch way to get the right version. You can find out if it is Server or Workstation (if that's important) in another process. I just didn't take time to add it.

We use this structure inside code to ensure compliance with requirements. I'm sure there are many more graceful ways but this does always work.

:: -------------------------------------
:: Check Windows Version
:: 5.0 = W2K
:: 5.1 = XP
:: 5.2 = Server 2K3
:: 6.0 = Vista or Server 2K8
:: 6.1 = Win7 or Server 2K8R2
:: 6.2 = Win8 or Server 2K12
:: 6.3 = Win8.1 or Server 2K12R2
:: 0.0 = Unknown or Unable to determine
:: --------------------------------------
echo OS Detection: Starting

ver | findstr /i "5\.0\."
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 2000
)
ver | findstr /i "5\.1\."
if %ERRORLEVEL% EQU 0 (
echo OS = Windows XP
)
ver | findstr /i "5\.2\."
if %ERRORLEVEL% EQU 0 (
echo OS = Server 2003
)
ver | findstr /i "6\.0\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Vista / Server 2008
)
ver | findstr /i "6\.1\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 7 / Server 2008R2
)
ver | findstr /i "6\.2\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 8 / Server 2012
)
ver | findstr /i "6\.3\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 8.1 / Server 2012R2
)


This second one isn't what was asked for but it may be useful for someone looking.

Here is a VBscript function that provides version info, including if it is the Server (vs. workstation).


private function GetOSVer()

dim strOsName: strOsName = ""
dim strOsVer: strOsVer = ""
dim strOsType: strOsType = ""

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOSes
strOsName = objOS.Caption
strOsVer = left(objOS.Version, 3)
Select Case strOsVer
case "5.0" 'Windows 2000
if instr(strOsName, "Server") then
strOsType = "W2K Server"
else
strOsType = "W2K Workstation"
end if
case "5.1" 'Windows XP 32bit
strOsType = "XP 32bit"
case "5.2" 'Windows 2003, 2003R2, XP 64bit
if instr(strOsName, "XP") then
strOsType = "XP 64bit"
elseif instr(strOsName, "R2") then
strOsType = "W2K3R2 Server"
else
strOsType = "W2K3 Server"
end if
case "6.0" 'Vista, Server 2008
if instr(strOsName, "Server") then
strOsType = "W2K8 Server"
else
strOsType = "Vista"
end if
case "6.1" 'Server 2008R2, Win7
if instr(strOsName, "Server") then
strOsType = "W2K8R2 Server"
else
strOsType = "Win7"
end if
case "6.2" 'Server 2012, Win8
if instr(strOsName, "Server") then
strOsType = "W2K12 Server"
else
strOsType = "Win8"
end if
case "6.3" 'Server 2012R2, Win8.1
if instr(strOsName, "Server") then
strOsType = "W2K12R2 Server"
else
strOsType = "Win8.1"
end if
case else 'Unknown OS
strOsType = "Unknown"
end select
Next
GetOSVer = strOsType
end Function 'GetOSVer
Reply

#8
Use a reg query and you will get an actual name:
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName

This would return "Windows 7 Professional", "Windows Server 2008 R2 Standard", etc. You can then combine with the ver command to get exact version.
Reply

#9
If you are looking to simply show windows version in a batch file such as Windows 10 etc.

you can simply use the `ver` command
just type `ver` and the windows version will be displayed


Reply

#10
wmic os get Caption,CSDVersion /value

This will do the job.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through