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?
This question is related to
windows
batch-file
operating-system
C:> cmd
Microsoft Windows [Version 10.0.17130.1233]
(c) 2018 Microsoft Corporation. All rights reserved.
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
)
These one-line commands have been tested on Windows XP, Server 2012, 7 and 10 (thank you Mad Tom Vane).
x.y
in a cmd
consolefor /f "tokens=4-7 delims=[.] " %i in ('ver') do @(if %i==Version (echo %j.%k) else (echo %i.%j))
x.y.z
for /f "tokens=4-7 delims=[.] " %i in ('ver') do @(if %i==Version (echo %j.%k.%l) else (echo %i.%j.%k))
%%
instead of single %
@echo off
for /f "tokens=4-7 delims=[.] " %%i in ('ver') do (if %%i==Version (set v=%%j.%%k) else (set v=%%i.%%j))
echo %v%
Be aware that the extracted version number does not always corresponds to the Windows name release. See below an extract from the full list of Microsoft Windows versions.
10.0
Windows 10
6.3
Windows Server 2012
6.3
Windows 8.1/!\
6.2
Windows 8/!\
6.1
Windows 7/!\
6.0
Windows Vista
5.2
Windows XP x64
5.1
Windows XP
5.0
Windows 2000
4.10
Windows 98
Please also up-vote answers from Agent Gibbs and peterbh as my answer is inspired from their ideas.
The Extract the full version x.y.z proposed by @olibre looks the most suitable to me. The only additional thing is to take into account other languages except English, as a string "version" may be completely different in a localized Windows. So, here is a batch file tested under Windows XP, 7, 8.1 and 10, using US and Russian locale:
for /f "tokens=4-5 delims= " %%i in ('ver') do (
if "%%j"=="" (set v=%%i) else (set v=%%j)
)
for /f "tokens=1-3 delims=.]" %%i in ("%v%") do (
set OS_VER_MAJOR=%%i
set OS_VER_MINOR=%%j
set OS_BUILD_NUM=%%k
)
echo Detected OS version: %OS_VER_MAJOR%.%OS_VER_MINOR%.%OS_BUILD_NUM%
The values of %OS_VER_MAJOR%, %OS_VER_MINOR% and %OS_BUILD_NUM% can be checked for exact version numbers.
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.
wmic os get Caption,CSDVersion /value
This will do the job.
So the answers I read here are mostly lengthy. The shortest answer by Alex works good, but not so clean.
Best part is that unlike others', it works fast.
I came up with my own one-liner:
FOR /F "usebackq tokens=3,4,5" %i IN (`REG query "hklm\software\microsoft\windows NT\CurrentVersion" /v ProductName`) DO echo %i %j %k
Above is for command prompt. To make it work in batch file, replace all % with %% to make it look like:
FOR /F "usebackq tokens=3,4,5" %%i IN (`REG query "hklm\software\microsoft\windows NT\CurrentVersion" /v ProductName`) DO echo %%i %%j %%k
In my computer, it simply produces Windows 10 Pro
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...
It's much easier (and faster) to get this information by only parsing the output of ver
:
@echo off
setlocal
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version%" == "10.0" echo Windows 10
if "%version%" == "6.3" echo Windows 8.1
if "%version%" == "6.2" echo Windows 8.
if "%version%" == "6.1" echo Windows 7.
if "%version%" == "6.0" echo Windows Vista.
rem etc etc
endlocal
This table on MSDN documents which version number corresponds to which Windows product version (this is where you get the 6.1 means Windows 7 information from).
The only drawback of this technique is that it cannot distinguish between the equivalent server and consumer versions of Windows.
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
The first line forces a WMI console installation if required on a new build. WMI always returns a consistent string of "Version=x.x.xxxx" so the token parsing is always the same for all Windows versions.
Parsing from the VER output has variable text preceding the version info, making token positions random. Delimiters are only the '=' and '.' characters.
The batch addition allows me to easily check versions as '510' (XP) up to '10000' for Win10. I don't use the Build value.
Setlocal EnableDelayedExpansion
wmic os get version /value 1>nul 2>nul
if %errorlevel% equ 0 (
for /F "tokens=2,3,4 delims==." %%A In ('wmic os get version /value') Do (
(Set /A "_MAJ=%%A")
(Set /A "_MIN=%%B")
(Set /A "_BLD=%%C")
)
(Set /A "_OSVERSION=!_MAJ!*100")
(Set /A "_OSVERSION+=!_MIN!*10")
)
endlocal
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
Here is another variant : some other solutions doesn't work with XP, this one does and was inspired by RLH solution.
This script will continue only if it detects the Windows version you want, in this example I want my script to run only in win 7, so to support other windows just change the GOTO :NOTTESTEDWIN
to GOTO :TESTEDWIN
ver | findstr /i "5\.0\." && (echo Windows 2000 & GOTO :NOTTESTEDWIN)
ver | findstr /i "5\.1\." && (echo Windows XP 32bit & GOTO :NOTTESTEDWIN)
ver | findstr /i "5\.2\." && (echo Windows XP x64 / Windows Server 2003 & GOTO :NOTTESTEDWIN)
ver | findstr /i "6\.0\." > nul && (echo Windows Vista / Server 2008 & GOTO :NOTTESTEDWIN)
ver | findstr /i "6\.1\." > nul && (echo Windows 7 / Server 2008R2 & GOTO :TESTEDWIN)
ver | findstr /i "6\.2\." > nul && (echo Windows 8 / Server 2012 & GOTO :NOTTESTEDWIN)
ver | findstr /i "6\.3\." > nul && (echo Windows 8.1 / Server 2012R2 & GOTO :NOTTESTEDWIN)
ver | findstr /i "10\.0\." > nul && (echo Windows 10 / Server 2016 & GOTO :NOTTESTEDWIN)
echo "Could not detect Windows version! exiting..."
color 4F & pause & exit /B 1
:NOTTESTEDWIN
echo "This is not a supported Windows version"
color 4F & pause & exit /B 1
:TESTEDWIN
REM put your code here
@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.
On more recent versions (win 7 onwards) you can have powershell do the job for you.
for /F %%i in ('powershell -command "& {$([System.Environment]::OSVersion.Version.Major),$([System.Environment]::OSVersion.Version.Minor) -join '.' }"') do set VER=%%i
goto %VER%
:6.1
REM specific code for windows 7
:10.0
REM specific code for windows 10
see this table for version numbers
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!
Source: Stackoverflow.com