While scripting it is necessary to determine what version of Windows a computer is running. The only issue is that Windows-10 and Server-2016 have the same version number .
This is the code currently. In this state, Windows10 and Server2016 are identified with the same number wmic os get
outputs 10.0.18329
(which doesn't work).
for /f "tokens=4-5 delims=. " %%i in ('ver') do set Operating=%%i.%%j
if "%Operating%" == "6.3" set Operating=Windows81
if "%Operating%" == "6.2" set Operating=Windows8
if "%Operating%" == "10.0" set Operating=Windows10
if "%Operating%" == "10.0" set Operating=Server2016
echo Is %Operating% your operating system?
What I would like to do is parse
wmic os get name
to get the OS name. This is showing some challenges because the output is multiple lines and there is an arbitrary number of characters before and after the OS name ('Windows 10').
This is the output of wmic os get name
on windows 10 pro:
Name
Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk1\Partition3
This is the output of wmic os get name
on Server 2016:
Name
Microsoft Windows Server 2016 Standard|C:\Windows|\Device\Harddisk0\Partition2
I know you've already chosen a solution, but if this is related to the last question you asked, now deleted , where you're only trying to determine between Windows 8
, Windows 8.1
, Windows 10
and Windows Server 2016
then I would have suggested something along these lines:
@Echo Off
Set "_S="
For /F "EOL=P Tokens=*" %%A In ('"WMIC OS Get ProductType,Version 2>Nul"'
) Do For /F "Tokens=1-3 Delims=. " %%B In ("%%A") Do Set /A _S=%%B,_V=%%C%%D
If Not Defined _S Exit /B
If %_V% Lss 62 Exit /B
If %_S% Equ 1 (If %_V% Equ 62 Set "_S=Windows8"
If %_V% Equ 63 Set "_S=Windows81"
If %_V% Equ 100 Set "_S=Windows10"
) Else If %_V% Equ 100 (Set "_S=Server2016") Else Exit /B
Set _S
Pause
Exit /B
[Edit /]
…and for a generic method, you may find this useful, especially the use of the Caption
property, (which I'd suggest is much better than using the Name
property, splitting it at the first pipe character, running it through another For
loop then removing one or more trailing space characters) .:
@For /F Tokens^=6Delims^=^" %%A In ('WMIC OS Get Caption/Format:MOF')Do Set "_S=%%A"
To test it just add a second line:
@Set _S&Pause
Note: This method may have issues on some earlier operating systems due to a bug preventing them finding the location of the .xsl
file. There are workarounds for this, either copy the .xsl
files from your 'language' directory up a level, or provide the full path to the .xsl
file, eg /Format:"%__AppDir__%wbem\\en-US\\MOF.xsl"
.
How about just straight forward...
@echo off
for /f "skip=1 delims=|" %%i in ('wmic os get name') do echo %%i & exit /b
This is simple:
@echo off
for /F "skip=1 tokens=2-4 delims=| " %%A IN ('wmic os get name') do (
set "os=%%A %%B %%C"
)
echo You are using %os%.
Another possible solution to include all versions of Windows:
@echo off
for /F "skip=1 delims=|" %%A IN ('wmic os get name') do (
for /F "delims=" %%B IN ("%%A") do (
set "os=%%B"
)
)
echo You are using %os:Microsoft =%.
pause
exit /b
Which will extract OS, substracting Microsoft<space>
at the end in both cases.
In the second case, two loops are used, since wmic
has unusual line endings ( <CR><CR><LF>
).
You can use the ver
command, here is an example:
@echo off
for /f "useback delimes=;" %%a in (`ver`) do set ver=%%a
echo The computer version your running is %ver%
Use the variable %ver%
as the version you are running, I use it to echo
the version but you can do anything with it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.