Skip to main content

How to Get the Operating System Version in a Batch Script

Knowing the version of the Windows operating system that a script is running on is a common and critical pre-flight check. Your script might depend on a command that is only available in newer versions of Windows, or you might need to apply a different set of configurations for Windows 10 versus Windows Server 2019.

This guide will teach you the two primary, built-in commands for retrieving the OS version: the simple and classic VER command, and the more powerful and detailed WMIC OS command, which is the recommended method for robust scripting.

Method 1: The VER Command (Simple but Limited)

The VER command is a classic, fast, and simple utility that prints the version number of the command interpreter (cmd.exe), which corresponds to the OS version.

Command:VER

Output (on a Windows 10/11 system)

Microsoft Windows [Version 10.0.19045.3570]

Limitations of VER:

  • It can be misleading. Since Windows Vista, the major version number has remained 6. Since Windows 10, it has been 10. This makes it difficult to distinguish between, for example, Windows 7 (6.1), Windows 8 (6.2), and Windows 10 (10.0).
  • It doesn't give the "friendly name." The output is a version number, not "Windows 11 Pro" or "Windows Server 2022".
  • Parsing is required. You have to parse the string to get just the numbers.

For these reasons, while VER is quick for a manual check, it's not ideal for robust scripting.

The WMIC (Windows Management Instrumentation Command-line) utility is the definitive tool for querying detailed system information. The OS alias lets us access a wealth of information about the operating system.

Command: WMIC OS GET Caption,Version /VALUE

  • OS: The WMI "class" representing the operating system.
  • GET Caption,Version: We are requesting two specific properties:
    • Caption: The user-friendly "marketing" name (e.g., "Microsoft Windows 11 Pro").
    • Version: The detailed build number (e.g., "10.0.22621").
  • /VALUE: Formats the output as a simple Key=Value list, which is perfect for parsing in a script.

Basic Example: Displaying the OS Version

This script demonstrates both methods.

@ECHO OFF
ECHO --- Method 1: Using the VER command ---
VER
ECHO.
ECHO --- Method 2: Using the WMIC command ---
WMIC OS GET Caption,Version /VALUE

Output:

--- Method 1: Using the VER command ---
Microsoft Windows [Version 10.0.19045.3570]

--- Method 2: Using the WMIC command ---
Caption=Microsoft Windows 10 Pro
Version=10.0.19045

The WMIC output is much cleaner and more descriptive, providing both the friendly name and the build number.

Storing the Version in a Variable

To use the OS version in your script's logic, you need to capture the output of these commands into variables.

Example of script using the recommended WMIC method:

@ECHO OFF
SETLOCAL
SET "OS_Name="
SET "OS_Version="

ECHO Getting OS information...

FOR /F "tokens=1,* delims==" %%A IN ('WMIC OS GET Caption^,Version /VALUE') DO (
IF "%%A"=="Caption" SET "OS_Name=%%B"
IF "%%A"=="Version" SET "OS_Version=%%B"
)

REM Clean the variables of any invisible trailing characters (a WMIC bug).
FOR %%N IN ("%OS_Name%") DO SET "OS_Name=%%~N"
FOR %%N IN ("%OS_Version%") DO SET "OS_Version=%%~N"

ECHO.
ECHO --- Captured Variables ---
ECHO OS Name: %OS_Name%
ECHO OS Version: %OS_Version%

ENDLOCAL
note

This script correctly parses the Key=Value output and stores the caption and version in separate, clean variables.

Common Pitfalls and How to Solve Them

  • Parsing VER is Unreliable: A script that tries to parse the output of VER can break on different language versions of Windows, as the surrounding text might change. Solution: Use WMIC, whose property names (Caption, Version) are language-independent.

  • WMIC Carriage Return Bug: As shown in the script above, WMIC output sometimes contains an extra, invisible carriage return character at the end of the value. This can break string comparisons. Solution: Always "clean" a variable captured from WMIC by re-assigning it in a simple FOR loop (e.g., FOR %%N IN ("%Var%") DO SET "Var=%%~N").

Practical Example: A Version-Gating Script

This script checks if it's running on a version of Windows 10 or newer. If not, it exits with an error. This is a common pre-flight check for a script that uses modern commands.

@ECHO OFF
SETLOCAL
SET "MajorVersion="

ECHO --- OS Version Compatibility Check ---
ECHO.

REM Get just the version number from WMIC.
FOR /F "tokens=2 delims==" %%V IN ('WMIC OS GET Version /VALUE') DO SET "VersionString=%%V"
FOR %%N IN ("%VersionString%") DO SET "VersionString=%%~N"

REM Get the major version number (the part before the first dot).
FOR /F "tokens=1 delims=." %%M IN ("%VersionString%") DO (
SET "MajorVersion=%%M"
)

ECHO Found OS Major Version: %MajorVersion%

IF %MajorVersion% LSS 10 (
ECHO [ERROR] This script requires Windows 10 or newer.
ECHO Detected an older operating system. Aborting.
PAUSE
GOTO :EOF
)

ECHO [SUCCESS] Compatible operating system detected.
ECHO Proceeding with main script logic...
REM (Your main script would go here)
TIMEOUT /T 5

ENDLOCAL

Conclusion

Getting the operating system version is an essential task for writing adaptable and robust batch scripts.

  • The VER command is quick and simple for a manual check but is not recommended for scripting due to its limited and potentially misleading output.
  • The WMIC OS GET Caption,Version command is the modern, reliable, and highly recommended method. It provides clean, detailed, and language-independent information that is perfect for parsing.

By using WMIC to query the OS version, you can easily create scripts that verify system compatibility before they run, preventing errors and providing clear feedback to the user.