Skip to main content

How to Get the Windows Build Number in Batch Script

The Windows Build Number (e.g., 19045 or 22621) is a precise identifier of your operating system's version and update level. Unlike the generic name "Windows 10," the build number tells you exactly which "Feature Update" (like 22H2) is installed. This is critical for developers and system administrators because many Batch commands and API features are only available in specific builds. By extracting the build number in your script, you can ensure compatibility and prevent execution on unsupported versions of Windows.

This guide explores how to retrieve this data using ver, wmic, and registry queries.

Why Check the Build Number?

  • Feature Feature Support: Identifying if the system supports modern commands like taskkill or specialized PowerShell modules.
  • Security Auditing: Verifying that a machine has reached the minimum build required for a specific security baseline.
  • Dynamic Logic: Running a different set of commands for Windows 10 versus Windows 11 based on the threshold build of 22000.
Major Version vs. Build
  • Major Version: Windows 10 or Windows 11.
  • Build Number: The specific iteration (e.g., 19045 for Win 10 22H2, 22621 for Win 11 22H2).

Method 1: Using the ver Command (Fastest)

The ver command is built directly into the command processor and is the fastest way to see the version string. However, it requires parsing to get just the number.

Basic Extraction

@echo off
for /f "tokens=6 delims=. " %%i in ('ver') do set "BUILD=%%i"
echo Your Windows Build Number is: %BUILD%
note

The tokens may shift slightly depending on your exact Windows version, so testing is required.

Method 2: Using the Registry (Most Reliable)

The registry stores the most accurate and up-to-date version information, including the "CurrentBuild" and "UBR" (Update Build Revision).

@echo off
set "REG_PATH=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion"

:: Query the build number
for /f "tokens=3" %%a in ('reg query "%REG_PATH%" /v CurrentBuild') do set "BUILD_NUM=%%a"

:: Query the UBR (The digits after the decimal point)
:: UBR is a REG_DWORD so reg query returns hex; set /a converts it to decimal
for /f "tokens=3" %%a in ('reg query "%REG_PATH%" /v UBR') do set /a "UBR_NUM=%%a"

echo Full OS Build: %BUILD_NUM%.%UBR_NUM%

Creating a Compatibility Check Script

A professional script uses the build number to handle different OS generations (Windows 10 vs. Windows 11).

@echo off
setlocal

:: 1. Get Build Number via WMI (cleaner for scripts)
:: Note: wmic output can contain a trailing carriage return;
:: the inner for /f strips it so the GEQ comparison stays numeric
for /f "tokens=2 delims==" %%i in ('wmic os get BuildNumber /value') do (
for /f "delims=" %%j in ("%%i") do set "VERSION_BUILD=%%j"
)

echo [PROCESS] Checking OS compatibility...
echo [INFO] Detected Build: %VERSION_BUILD%

:: 2. Logic Check
:: Windows 11 starts at Build 22000
if %VERSION_BUILD% GEQ 22000 (
echo [STATUS] Operating System: Windows 11
goto :Win11Tasks
) else if %VERSION_BUILD% GEQ 10240 (
echo [STATUS] Operating System: Windows 10
goto :Win10Tasks
) else (
echo [ERROR] This script requires at least Windows 10.
pause
exit /b 1
)

:Win11Tasks
echo Updating UI for Windows 11...
:: Run Win11 commands here
goto :End

:Win10Tasks
echo Running legacy Windows 10 maintenance...
:: Run Win10 commands here
goto :End

:End
pause

Common Pitfalls and How to Avoid Them

String vs. Number Comparison

In Batch, when you compare with GEQ (Greater than or Equal), the variables must be treated as numbers.

Wrong Way:

if "Build 19045" GEQ "22000" ...
:: String comparison will fail to give the correct mathematical result.

Correct Way: Always use a FOR loop to strip away everything except the digits before performing the comparison.

Regional Variations

On some international versions of Windows, the ver command output might include extra words or translated text (e.g., "Versione...").

SEO and UX Tip

For a script that needs to work globally, always use WMIC or the Registry. These sources are language-independent and return raw numerical data that is much easier to parse reliably across different locales.

Advanced: Getting the "Release Id"

Sometimes the build number is too specific, and you just want the marketing version (like 22H2).

:: Query for Release ID (e.g., 20H2) or DisplayVersion (e.g., 22H2)
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "DisplayVersion"
Administrative Rights

Querying the build number from the registry or WMI does NOT require administrative rights. You can run these detection commands as a standard user, making them safe for pre-execution checks.

Conclusion

Retrieving the Windows Build Number via Batch script is a powerful way to add "version awareness" to your automation projects. By mapping build numbers to OS generations (like the 22000 threshold for Windows 11), you can create intelligent scripts that adjust their behavior to match the system's capabilities. This professional approach to scripting ensures a higher success rate for your tools, prevents runtime errors on older systems, and allows you to target the specific features of modern Windows updates with confidence.