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

#11
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
Reply

#12
<sub>These one-line commands have been tested on Windows XP, Server 2012, 7 and 10 (thank you [Mad Tom Vane](

[To see links please register here]

;

### Extract version `x.y` in a `cmd` console

for /f "tokens=4-7 delims=[.] " %i in ('ver') do @(if %i==Version (echo %j.%k) else (echo %i.%j))


### Extract the full version `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))


### In a batch script use `%%` 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%

### Version is not always consistent to brand name number

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](

[To see links please register here]

).

> `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

<sup>Please also up-vote answers from [Agent Gibbs](

[To see links please register here]

) and [peterbh](

[To see links please register here]

) as my answer is inspired from their ideas.</sup>
Reply

#13
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][1] 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.


[1]:

[To see links please register here]

Reply

#14
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**
Reply

#15
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.
Reply

#16
On more recent versions (win 7 onwards) you can have [powershell do the job for you.][1]

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][2] for version numbers



[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#17
Here is another variant : some other solutions doesn't work with XP, this one does and was inspired by [RLH solution][1].

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`

```bash
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
```

[1]:

[To see links please register here]

Reply

#18
None of the previous answers work on Win9x, and will soon fail with w10 22h1.
First 4 lines are win3.x compatible ("set /P"; "for /F" are illegal there).
It then saves the version on wver var, used on comparisons: The leading space on search string is essential to catch the begining of the number in the ver string (ie: "4." matches w10 21h2 string: "Microsoft Windows [Version 10.0.19044.1348]"). Most of previous solutions will soon fail with [future] windows versions. Next w10 22h1 string will be: "Microsoft Windows [Version 10.0.19045.1xxx]", that will match the "5.1" XP search string.

@echo off
ver | find " 3." >nul
if not ERRORLEVEL 1 goto :wold
ver | find " 4." >nul
if not ERRORLEVEL 1 goto :wold
rem put version on wver variable
for /F "delims=" %%a in ('ver') do set wver=%%a
echo Windows Version: %wver%
rem Do all >= w2000/xp tasks here
if not "%wver: 5.=%" == "%wver%" ( echo 5.0=2k 5.1=xp 5.2=xp64/2k3 & goto fin )
rem Do all >= vista tasks here
if not "%wver: 6.0=%" == "%wver%" ( echo vista/2k8 & goto fin )
rem Do all >= w7 tasks here
if not "%wver: 6.1=%" == "%wver%" ( echo w7/2k8r2 & goto fin )
rem Do all >= w8 tasks here
if not "%wver: 6.=%" == "%wver%" ( echo 6.2=w8/2012 6.3=w8.1/2012r2 & goto fin )
rem Do all >= w10 tasks here
if not "%wver: 10.=%" == "%wver%" ( echo w10.x & goto fin )
rem Do all >= w11 tasks here
if not "%wver: 11.=%" == "%wver%" ( echo w11.x & goto fin )
rem Do all >= w12? tasks here
goto fin
:wold
echo Unsupported windows 3.x 9x me nt3 nt4...
:fin

Reply

#19
I use this:

@echo off & @echo.
setlocal
@echo Getting Windows version... & @echo.
@echo. > Windows_version_and_Key.txt
@echo Windows Version: >> Windows_version_and_Key.txt
for /f "tokens=* delims=. " %%i in ('ver') do set VERSION=%%i
@echo %VERSION% >> Windows_version_and_Key.txt
@echo. >> Windows_version_and_Key.txt
@echo. >> Windows_version_and_Key.txt
wmic os get Caption >> Windows_version_and_Key.txt
@echo. >> Windows_version_and_Key.txt
@echo Getting Windows license KEY...
@echo License KEY: >> Windows_version_and_Key.txt
wmic path softwarelicensingservice get OA3xOriginalProductKey >> Windows_version_and_Key.txt
powershell -c "(Get-Content .\Windows_version_and_Key.txt) -replace '\x00+', '' | Set-Content .\Windows_version_and_Key.txt"
type Windows_version_and_Key.txt
@echo. & @echo. & @echo Done! Press any key to Exit!
endlocal
pause
exit


It gives your windows key in case of you already lost it and need it.

All will be writed in a text file at end.
Reply

#20
Here is the batch file I finally came up with to determine if the operating system was less than Windows 11 (including my debugging `echo` statements to help users track what's happening). (My use case is that I'm customizing toolbars in the Windows taskbar, and toolbars are not Win11-compatible at the moment, so I don't want to run it on Win11 PC's.)

It can be modified to accommodate to whatever windows versions you want to filter out by modifying the `LSS 11` line.

It stores the entire output of `wmic os get Caption | findstr /v Caption` in `%%i`. It then loops through each space-delimited "token" within `%%i` until it hits an integer (detected with regex). If the integer is less than (`LSS`) 11, it sets `lesthaneleven` to `true`. (It skips comparing any proceeding integers.)

This is more verbose/juvenile code than other answers, but I'm happy with it, because I feel it's pretty easy to read and thus modifiable for others' needs.

@echo off

for /f "tokens=*" %%i in ('wmic os get Caption ^| findstr /v Caption') do (
for %%j in (%%i) do (
echo token: %%j
if not defined lessthaneleven (
echo("%%j"|findstr "^[\"][-][1-9][0-9]*[\"]$ ^[\"][1-9][0-9]*[\"]$ ^[\"]0[\"]$">nul&& (
if %%j LSS 11 (
set "lessthaneleven=true"
) else (
set "lessthaneleven=false"
)
) || (
echo token not numeric
)
)
)
)
)
if "%lessthaneleven%" == "true" (
echo less than eleven
) else (
echo not less
)
set lessthaneleven=

[regex credit][1]


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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