12

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
Martin Rezyne
  • 445
  • 3
  • 9
  • 24

6 Answers6

21
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
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
  • Thank you. This is exactly what i needed – Martin Rezyne Dec 16 '15 at 18:20
  • This method does not work for Windows XP because `ver` outputs `Microsoft Windows XP [Version 5.1.2600]` and therefore `[Version.5` is assigned to variable `VERSION`. However, it works for just checking if operating system is Windows 10 as asked by [Martin Rezyne](http://stackoverflow.com/users/2357522/martin-rezyne). – Mofi Dec 16 '15 at 18:20
  • 1
    @Mofi - regardless, the value of %VERSION% will not be `10.0` and so it can be used to check whether or not you're running Windows 10. It won't help with getting the actual version, but that's not what the question is asking. – SomethingDark Dec 16 '15 at 19:07
  • The answer is incomplete, missing the part of "else echo I am another os" – acgbox May 25 '19 at 00:42
6

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)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • This method works even for Windows XP Professional which is now not longer supported by Microsoft. But it is slow in comparison to using output of command `ver` as `wmic` needs always more than 1 second to finish. – Mofi Dec 16 '15 at 18:16
3

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
MarekJ47
  • 143
  • 1
  • 4
0

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
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
0

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
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Problem: the output of `systeminfo` is language-dependent, so this works only for Engish versions of Windows. (Besides that, `systeminfo` is quite slow compared to the aproaches in the other answers) – Stephan Jan 12 '20 at 16:35
  • But the OP's locale seems to be English. – Wasif Jan 12 '20 at 16:50
0

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