The reg query way suggested all output a little garbage.
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" | findstr ReleaseId
Output:
ReleaseId    REG_SZ    2009
Using a for loop with tokens will output clean information.
for /f "tokens=3" %i in ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" ^| findstr ReleaseId') do echo %i
Output:
2009
The tokens=3 refers to the third word from the original output.
You will need to double the % if running inside a bat file.
You can set the output as a variable by replacing echo %i with set build=%i
Also remember to escape ^ any special characters.
Lastly look at HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion for the string that has the required value. You may need to adjust the token count.