Martin Rezyne
Martin Rezyne

Reputation: 445

Batch file: Check if OS is Windows 10

I want to make a batch file which will do the following operation: checks if the running OS is windows. If it is than it should print Hello. Im win 10 else should print other message. How can i do this if condition?

Pseudocode:

if OS == Win10 then
   echo Hello im win 10
else
   echo I am another os

Upvotes: 12

Views: 34583

Answers (7)

balintn
balintn

Reputation: 1443

Based on all the great answers coming before me, this one can detect Windows 11 as well.

@echo off
setlocal
for /f "tokens=2 delims=[]" %%i in ('ver') do set FULLVERSION=%%i
for /f "tokens=2-5 delims=. " %%i in ("%FULLVERSION%") do set         FULLVERSION=%%i.%%j.%%k.%%l
echo Full version: %FULLVERSION%

for /f "tokens=1-2 delims=. " %%i in ("%FULLVERSION%") do set VERSION=%%i.%%j
echo Version: %VERSION%

for /f "tokens=3-3 delims=. " %%i in ("%FULLVERSION%") do set BUILDMAJOR=%%i
echo Build major: %BUILDMAJOR%

set VERSIONNAME=Unknown
if "%VERSION%" == "5.00" set VERSIONNAME=2000
if "%VERSION%" == "5.0" set VERSIONNAME=2000
if "%VERSION%" == "5.1" set VERSIONNAME=XP
if "%VERSION%" == "5.2" set VERSIONNAME=Server 2003
if "%VERSION%" == "6.0" set VERSIONNAME=Vista
if "%VERSION%" == "6.1" set VERSIONNAME=7
if "%VERSION%" == "6.2" set VERSIONNAME=8
if "%VERSION%" == "6.3" set VERSIONNAME=8.1
if "%VERSION%" == "6.4" set VERSIONNAME=10
if "%VERSION%" == "10.0" (
    if %BUILDMAJOR% GEQ 22000 set VERSIONNAME=11
    set VERSIONNAME=10
)
echo Windows %VERSIONNAME%
endlocal

Upvotes: 0

deathToSkyNet
deathToSkyNet

Reputation: 1

I know this is an old post, but I wanted to post by verison of the above. Using system info is WAY too slow for me; and I wanted more server version detections

@echo off

setlocal

REM had to wrap this to catch errors on server 2008
(for /f "tokens=2 delims=," %%i in ('wmic os get caption^,version /format:csv') do set tmp=%%i)>nul 2>&1

if "%tmp%" equ "" (
    set os=Error During OS lookup
) else (
    set os=%tmp%
)

echo %os%


    echo %os% | findstr /C:"Server" 1>nul
    if errorlevel 1 (       
        net statistics server>nul 2>&1
        if errorlevel 1 (
            set isServer=0
        ) else (
            # rem for server 2008, but also final check
            set isServer=1
        )
    ) else (
        set isServer=1
    )



for /f "tokens=2 delims=[]" %%i in ('ver') do set VERSION=%%i
for /f "tokens=2-3 delims=. " %%i in ("%VERSION%") do set VERSION=%%i.%%j
if "%VERSION%" == "5.00" echo Windows 2000
if "%VERSION%" == "5.0" echo Windows 2000
if "%VERSION%" == "5.1" echo Windows XP
if "%VERSION%" == "5.2" echo Windows Server 2003
if "%VERSION%" == "6.0" (
    if "%isServer%" equ "0" (
        echo Windows Vista
    ) else (
        echo Server 2008 Enterprise
    )

)
if "%VERSION%" == "6.1" (
    if "%isServer%" equ "0" (
        echo Windows 7
    ) else (
        echo Server 2008
    )

)
if "%VERSION%" == "6.2" echo Windows 8
if "%VERSION%" == "6.3" (
    if "%isServer%" equ "0" (
        echo Windows 8.1
    ) else (
        echo Server 2012
    )
)
if "%VERSION%" == "6.4" echo Windows 10
if "%VERSION%" == "10.0" (
    if "%isServer%" equ "0" (
        echo Windows 10
    ) else (
        echo %os% | findstr /C:"2016" 1>nul
        if not errorlevel 1 (
            echo Server 2016
        )
        echo %os% | findstr /C:"2019" 1>nul
        if not errorlevel 1 (
            echo Server 2019
        )
    )
)

echo %VERSION%
echo %isServer%
endlocal

pause

Upvotes: 0

T3RR0R
T3RR0R

Reputation: 2951

Quick and easy...

wmic OS get OSArchitecture,caption | FIND "10" >nul || GOTO Not_Win10

:: Your Program...

:Not_Win10
ECHO Not Compatible
pause
exit

Without using a label / Goto:

wmic OS get OSArchitecture,caption | FIND "10" >nul || ECHO System not Windows 10 && TIMEOUT 3 >nul && EXIT

ECHO System is Windows 10.
pause
::: Your Program

Upvotes: 0

wasif
wasif

Reputation: 15488

Another easy way:

@Echo Off
SystemInfo | Find "OS Name:" >tmp.txt
Type tmp.txt | Find "10" >nul 2>&1  || (
  Del tmp.txt
  Echo OS Not Windows 10
  Pause 
  Exit /B
)

Del tmp.txt

:: Main Program

Upvotes: 0

MarekJ47
MarekJ47

Reputation: 143

I improved Vertexwahn's script to support more Windows versions:

setlocal
for /f "tokens=2 delims=[]" %%i in ('ver') do set VERSION=%%i
for /f "tokens=2-3 delims=. " %%i in ("%VERSION%") do set VERSION=%%i.%%j
if "%VERSION%" == "5.00" echo Windows 2000
if "%VERSION%" == "5.0" echo Windows 2000
if "%VERSION%" == "5.1" echo Windows XP
if "%VERSION%" == "5.2" echo Windows Server 2003
if "%VERSION%" == "6.0" echo Windows Vista
if "%VERSION%" == "6.1" echo Windows 7
if "%VERSION%" == "6.2" echo Windows 8
if "%VERSION%" == "6.3" echo Windows 8.1
if "%VERSION%" == "6.4" echo Windows 10
if "%VERSION%" == "10.0" echo Windows 10
echo %VERSION%
endlocal

Upvotes: 3

Stephan
Stephan

Reputation: 56198

if you want it a bit more detailed:

for /f "tokens=2 delims=," %%i in ('wmic os get caption^,version /format:csv') do set os=%%i
echo Hello, I am %os%

or to just meet your requirements:

for /f "tokens=2 delims=," %%i in ('wmic os get caption^,version /format:csv') do set os=%%i
echo %os%|find " 10 ">nul &&echo Hello I'm Windows 10||echo I am another os

(the ,version ensures, your desired string is not the last token, which contains that ugly wmic line ending)

Upvotes: 6

Vertexwahn
Vertexwahn

Reputation: 8152

setlocal
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
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.
if "%version%" == "10.0" echo Windows 10.
echo %version%
rem etc etc
endlocal

Upvotes: 21

Related Questions