Setting a Batch Variable equal to the OS Name:

Please help me set a variable equal to the OS Name of the machine the batch script is running on.

For example, I need SET localOS=(the name of the os running on the machine ie Microsoft Windows 7 Professional, Microsoft Windows server R2 2008)

I've tried a few for loops with systeminfo, but I haven't been able to narrow it down.

systeminfo | findstr /B /C:"OS Name"

OS Name: Microsoft Windows 7 Professional

Okay now just need to to parse out the first part and set the variable equal to the second.

Figured out a loop to do it for me.

Thread no longer needed.

A bit late on this, but can also get the same info via WMIC since systeminfo takes forever to load. Also (should) work in WinPE if the wmic package is installed on it.

temp.cmd:

for /f "tokens=1* delims== skip=2" %%i in ('wmic os get caption /format:list') do (set localOS=%%j
goto next1)
:next1
echo %localos%

1 Like

Awesome! Thanks @Peanut253!!!

My next mission is to build an array of "accepted OSes" and run a for loop which iterates of the array of "acceptedOSes" and compares it to the localOS variable we just created. If localOS matches any"acceptedOS" then the script continues else it ends the script.

Any advice?

I already did something similar so I can give you advice but the way you're doing it is a bit difficult. Let me explain.

My next mission is to build an array of "accepted OSes" and run a for loop which iterates of the array of "acceptedOSes" and compares it to the localOS variable we just created. If localOS matches any"acceptedOS" [sic] then the script continues else it ends the script.

The Batch language does not support true arrays since it's more of a basic interpreter than a real programming language. The way to do it in batch is to figure out what the environment is, what the valid choices are and then exiting the script if any invalid anythings are found in a very linear fashion.

This dynamic building of arrays in batch is sorta technically possible but enormously difficult to implement and use since they are not naively supported and kinda pointless to be honest. I did something similar, let me know if you want the code for it but the way I would write what you are trying to do atm is as follows:

1) Windows has Versions (Win XP, Vista, 8) which are internally just build numbers like 6.1.7601
or you could shorten it to 6.0; 6.1; 6.2; 6.3; 10.0 from: "wmic os get version /format:list" or diskpart with a for loop using the . as a delim

2) Architectures (x86, AMD64, ARM)
From the %processor_architecture%

3) and Editions (Ultimate, Home, Pro, Enterprise, Enterprise S, Basic)
from: wmic os get Caption /format:list

Comparing any of the above is easy but not if you combine them like "Microsoft Windows 7 Professional" since then you're comparing both a Version and an Edition at the same time. You can just build really long valid version lists and compare extremely linearly but it's more scalable to split them up and compare them independently.

I recommend figuring out what are all the valid choices and looking for patterns.
Are all Win7 Editions valid but only x86? Is Win Vista supported or Win 10, but not Win 8.1?

Then you can do something like this. This will only find x86 versions of certain windows versions valid.

::compare the Architecture first
if /i "%processor_architecture%" equ "x86" set architecture=x86
if /i "%processor_architecture%" equ "AMD64" set architecture=x64

set IsArchitectureValid=false
if /i "%architecture%" equ "x86" set IsArchitectureValid=true

if /i "%IsArchitectureValid%" neq "true" (echo invalid OS Only 32-bit systems are supported
echo current OS architecture: %architecture%
goto end)

::compare the Version next
for /f "tokens=1* delims== skip=2" %%i in ('wmic os get version /format:list') do (set rawWimOSVersion=%%j
goto next1)
:next1
echo %rawWimOSVersion%
for /f "delims=. tokens=1,2" %%i in ("%rawWimOSVersion%") do set rawWimOSVersion=%%i.%%j
echo %rawWimOSVersion%

if /i "%rawWimOSVersion%" equ "6.0" (set OSVersion=Vista)
if /i "%rawWimOSVersion%" equ "6.1" (set OSVersion=7)
if /i "%rawWimOSVersion%" equ "6.2" (set OSVersion=8)
if /i "%rawWimOSVersion%" equ "6.3" (set OSVersion=81)
if /i "%rawWimOSVersion%" equ "10.0" (set OSVersion=10)

set IsVersionValid=false
if /i "%OSVersion%" equ "7" set IsVersionValid=true
if /i "%OSVersion%" equ "8" set IsVersionValid=true
if /i "%OSVersion%" equ "81" set IsVersionValid=true

if /i "%isOSValid%" neq "true" (echo invalid OS, Only 7 to Win 8.1 are supported
echo current OS version is Windows %OSVersion%
goto end)

::valid OS code goes here

:end
echo script done

Editions do not really matter as far as programs are usually concerned but if you want to compare them then filter out the 4th entry from the "wmic os get Caption /format:list" command.

for /f "tokens=1* delims== skip=2" %%i in ('wmic os get caption /format:list') do (set localOS=%%j
goto next1)
:next1
echo %localos%
for /f "tokens=4" %%i in ("%localos%") do echo Edition is: %%i

And then it would be possible to figure out the valid editions.

::Non-WMIC way to get Windows Version (for WinPE without WMI packages)

diskpart /s nonexist.txt > temp.txt
for /f "tokens=4" %%i in ('find /n "version" temp.txt') do set rawVersion=%%i
for /f "tokens=1,2 delims=." %%i in ("%rawVersion%") do set rawVersion=%%i.%%j

set winVersionNumber=%rawVersion%
if /i "%rawVersion%" equ "6.0" (set winVersionNumber=2.0
set winOSVersion=Vista)
if /i "%rawVersion%" equ "6.1" (set winVersionNumber=3.x
set winOSVersion=7)
if /i "%rawVersion%" equ "6.2" (set winVersionNumber=4.0
set winOSVersion=8)
if /i "%rawVersion%" equ "6.3" (set winVersionNumber=5.x
set winOSVersion=81)
if /i "%rawVersion%" equ "10.0" (set winVersionNumber=10.0
set winOSVersion=10)