I need to get the OS version with a batch file. I 've seen a lot of examples online, many uses something like this code:
我需要用批处理文件获取操作系统版本。我在网上看到过很多这样的例子,很多都是用这样的代码:
@echo off
ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp
if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit
systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i
echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7
echo %vers% | find "Windows Vista" > nul
if %ERRORLEVEL% == 0 goto ver_vista
goto warnthenexit
:ver_7
:Run Windows 7 specific commands here.
echo Windows 7
goto exit
:ver_vista
:Run Windows Vista specific commands here.
echo Windows Vista
goto exit
:ver_xp
:Run Windows XP specific commands here.
echo Windows XP
goto exit
:warnthenexit
echo Machine undetermined.
:exit
The problem is when I execute this on Vista or Windows 7 I get the message
问题是,当我在Vista或Windows 7上执行这个操作时,我得到了消息。
Machine undetermined
机待定
Is there any other way to do what I want?
还有别的方法可以做我想做的吗?
13 个解决方案
#1
11
Have you tried using the wmic
commands?
你试过使用wmic命令吗?
Try wmic os get version
试试wmic os得到版本。
This will give you the version number in a command line, then you just need to integrate into the batch file.
这将在命令行中给出版本号,然后只需要集成到批处理文件中。
#2
42
It's much easier (and faster) to get this information by only parsing the output of ver
:
通过解析ver的输出来获得这些信息要容易得多(而且更快)。
@echo off
setlocal
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version%" == "6.3" echo Windows 8.1
if "%version%" == "6.2" echo Windows 8.
if "%version%" == "6.1" echo Windows 7.
if "%version%" == "6.0" echo Windows Vista.
rem etc etc
endlocal
This table on MSDN documents which version number corresponds to which Windows product version (this is where you get the 6.1 means Windows 7 information from).
此表在MSDN文档中,版本号对应的是Windows产品版本(这是您从这里得到的6.1意味着Windows 7信息)。
The only drawback of this technique is that it cannot distinguish between the equivalent server and consumer versions of Windows.
这种技术的唯一缺点是它不能区分Windows的等效服务器和消费者版本。
#3
12
These one-line commands have been tested on Windows XP, Server 2012, 7 and 10 (thank you Mad Tom Vane).
这些单行命令已经在Windows XP、Server 2012、7和10上进行了测试(感谢Mad Tom Vane)。
Extract version x.y
in a cmd
console
for /f "tokens=4-7 delims=[.] " %i in ('ver') do @(if %i==Version (echo %j.%k) else (echo %i.%j))
Extract the full version x.y.z
for /f "tokens=4-7 delims=[.] " %i in ('ver') do @(if %i==Version (echo %j.%k.%l) else (echo %i.%j.%k))
In a batch script use %%
instead of single %
@echo off
for /f "tokens=4-7 delims=[.] " %%i in ('ver') do (if %%i==Version (set v=%%j.%%k) else (set v=%%i.%%j))
echo %v%
Version is not always consistent to brand name number
Be aware that the extracted version number does not always corresponds to the Windows name release. See below an extract from the full list of Microsoft Windows versions.
请注意,提取的版本号并不总是对应于Windows名称的发布。从微软Windows版本的完整列表中查看。
10.0
Windows 106.3
Windows Server 20126.3
Windows 8.1/!\
6.2
Windows 8/!\
6.1
Windows 7/!\
6.0
Windows Vista5.2
Windows XP x645.1
Windows XP5.0
Windows 20004.10
Windows 9810.0 Windows 10 6.3 Windows Server 2012 6.3 Windows 8.1 /!\ 6.2 Windows 8 /!\ 6.1 Windows 7 /!windowsvista 5.2 windowsxp x64 5.1 Windows XP 5.0视窗2000 4.10视窗98。
Please also up-vote answers from Agent Gibbs and peterbh as my answer is inspired from their ideas.
请从吉布斯和彼得·波黑的回答中得到答案,因为我的回答是来自他们的想法。
#4
5
I know it's an old question but I thought these were useful enough to put here for people searching.
我知道这是一个老问题,但我认为这些都很有用,可以供人们搜索。
This first one is a simple batch way to get the right version. You can find out if it is Server or Workstation (if that's important) in another process. I just didn't take time to add it.
第一个是简单的批处理方法,以获得正确的版本。在另一个过程中,您可以发现它是否是服务器或工作站(如果这很重要)。我只是没花时间把它加进去。
We use this structure inside code to ensure compliance with requirements. I'm sure there are many more graceful ways but this does always work.
我们在代码中使用这个结构来确保符合要求。我相信有很多更优雅的方式,但这总是有效的。
:: -------------------------------------
:: Check Windows Version
:: 5.0 = W2K
:: 5.1 = XP
:: 5.2 = Server 2K3
:: 6.0 = Vista or Server 2K8
:: 6.1 = Win7 or Server 2K8R2
:: 6.2 = Win8 or Server 2K12
:: 6.3 = Win8.1 or Server 2K12R2
:: 0.0 = Unknown or Unable to determine
:: --------------------------------------
echo OS Detection: Starting
ver | findstr /i "5\.0\."
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 2000
)
ver | findstr /i "5\.1\."
if %ERRORLEVEL% EQU 0 (
echo OS = Windows XP
)
ver | findstr /i "5\.2\."
if %ERRORLEVEL% EQU 0 (
echo OS = Server 2003
)
ver | findstr /i "6\.0\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Vista / Server 2008
)
ver | findstr /i "6\.1\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 7 / Server 2008R2
)
ver | findstr /i "6\.2\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 8 / Server 2012
)
ver | findstr /i "6\.3\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 8.1 / Server 2012R2
)
This second one isn't what was asked for but it may be useful for someone looking.
这第二个不是我们要的,但它可能对一个人有用。
Here is a VBscript function that provides version info, including if it is the Server (vs. workstation).
这是一个提供版本信息的VBscript函数,包括如果它是服务器(与工作站)。
private function GetOSVer()
dim strOsName: strOsName = ""
dim strOsVer: strOsVer = ""
dim strOsType: strOsType = ""
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOSes
strOsName = objOS.Caption
strOsVer = left(objOS.Version, 3)
Select Case strOsVer
case "5.0" 'Windows 2000
if instr(strOsName, "Server") then
strOsType = "W2K Server"
else
strOsType = "W2K Workstation"
end if
case "5.1" 'Windows XP 32bit
strOsType = "XP 32bit"
case "5.2" 'Windows 2003, 2003R2, XP 64bit
if instr(strOsName, "XP") then
strOsType = "XP 64bit"
elseif instr(strOsName, "R2") then
strOsType = "W2K3R2 Server"
else
strOsType = "W2K3 Server"
end if
case "6.0" 'Vista, Server 2008
if instr(strOsName, "Server") then
strOsType = "W2K8 Server"
else
strOsType = "Vista"
end if
case "6.1" 'Server 2008R2, Win7
if instr(strOsName, "Server") then
strOsType = "W2K8R2 Server"
else
strOsType = "Win7"
end if
case "6.2" 'Server 2012, Win8
if instr(strOsName, "Server") then
strOsType = "W2K12 Server"
else
strOsType = "Win8"
end if
case "6.3" 'Server 2012R2, Win8.1
if instr(strOsName, "Server") then
strOsType = "W2K12R2 Server"
else
strOsType = "Win8.1"
end if
case else 'Unknown OS
strOsType = "Unknown"
end select
Next
GetOSVer = strOsType
end Function 'GetOSVer
#5
3
This will return 5.1 for xp or 6/1 for windows 7
这将返回5.1的xp或6/1的windows 7。
for /f "tokens=4-7 delims=[.] " %%i in ('ver') do (
if %%i == Version set OSVersion=%%j.%%k
if %%i neq Version set OSVersion=%%i.%%j
)
#6
2
Here's my one liner to determine the windows version:
这是我的一个班轮来确定windows版本:
for /f "tokens=1-9" %%a in ('"systeminfo | find /i "OS Name""') do (set ver=%%e & echo %ver%)
This returns the windows version, i.e., XP, Vista, 7, and sets the value of "ver" to equal the same...
这将返回windows版本,即,XP, Vista, 7,并设置“ver”的值相等…
#7
2
Actually, that one liner doesn't work for windows xp since the ver contains xp in the string. Instead of 5.1 which you want, you would get [Version.5 because of the added token.
实际上,这一行代码并不适用于windows xp,因为它包含了字符串中的xp。而不是你想要的5.1,你会得到[版本。5因为添加了令牌。
I modified the command to look like this: for /f "tokens=4-6 delims=[. " %%i in ('ver') do set VERSION=%%i.%%j
我修改了命令如下:for /f“令牌=4-6 delims=[。“%%i in ('ver') do set VERSION=%%i.%% i.% i.%% %i %%。”
This will output Version.5 for xp systems which you can use to indentify said system inside a batch file. Sadly, this means the command cannot differentiate between 32bit
and 64bit
build since it doesn't read the .2 from 5.2 that denotes 64bit
XP.
这将输出版本。5用于xp系统,您可以使用该系统在批处理文件中识别该系统。遗憾的是,这意味着该命令不能区分32位和64位构建,因为它没有从5.2中读取.2,表示64位XP。
You could make it assign %%k
that token but doing so would make this script not detect windows vista, 7, or 8 properly as they have one token less in their ver string. Hope this helps!
您可以让它为标记指定%%k,但是这样做将使该脚本不能正确地检测windowsvista、7或8,因为它们的ver字符串中有一个标记更少。希望这可以帮助!
#8
2
Use a reg query and you will get an actual name: reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName
使用一个reg查询,你会得到一个实际的名字:reg查询“HKLM\软件\微软\Windows NT\CurrentVersion”/v ProductName。
This would return "Windows 7 Professional", "Windows Server 2008 R2 Standard", etc. You can then combine with the ver command to get exact version.
这将返回“Windows 7专业”,“Windows Server 2008 R2标准”,等等。然后你可以与ver命令结合,得到准确的版本。
#9
1
@echo off
for /f "tokens=2 delims=:" %%a in ('systeminfo ^| find "OS Name"') do set OS_Name=%%a
for /f "tokens=* delims= " %%a in ("%OS_Name%") do set OS_Name=%%a
for /f "tokens=3 delims= " %%a in ("%OS_Name%") do set OS_Name=%%a
if "%os_name%"=="XP" set version=XP
if "%os_name%"=="7" set version=7
This will grab the OS name as "7" or "XP"
这将抓取OS名称为“7”或“XP”
Then you can use this in a variable to do certain commands based on the version of windows.
然后,您可以在一个变量中使用它来执行基于windows版本的某些命令。
#10
1
If you are looking to simply show windows version in a batch file such as Windows 10 etc.
如果您想要简单地在批处理文件中显示windows版本,比如windows 10等等。
you can simply use the ver
command just type ver
and the windows version will be displayed
您可以简单地使用ver命令,只需键入ver,并且将显示windows版本。
#11
1
wmic os get Caption,CSDVersion /value
This will do the job.
这样做就行了。
#12
1
Here is another variant : some other solutions doesn't work with XP, this one does and was inspired by RLH solution.
这是另一种变体:一些其他的解决方案与XP不兼容,这一方法是由RLH解决方案所启发的。
This script will continue only if it detects the Windows version you want, in this example I want my script to run only in win 7, so to support other windows just change the GOTO :NOTTESTEDWIN
to GOTO :TESTEDWIN
这个脚本只会在它检测到你想要的Windows版本时才会继续,在这个例子中,我希望我的脚本只在win 7中运行,所以支持其他窗口只会改变GOTO:NOTTESTEDWIN:TESTEDWIN。
ver | findstr /i "5\.0\." && (echo Windows 2000 & GOTO :NOTTESTEDWIN)
ver | findstr /i "5\.1\." && (echo Windows XP 32bit & GOTO :NOTTESTEDWIN)
ver | findstr /i "5\.2\." && (echo Windows XP x64 / Windows Server 2003 & GOTO :NOTTESTEDWIN)
ver | findstr /i "6\.0\." > nul && (echo Windows Vista / Server 2008 & GOTO :NOTTESTEDWIN)
ver | findstr /i "6\.1\." > nul && (echo Windows 7 / Server 2008R2 & GOTO :TESTEDWIN)
ver | findstr /i "6\.2\." > nul && (echo Windows 8 / Server 2012 & GOTO :NOTTESTEDWIN)
ver | findstr /i "6\.3\." > nul && (echo Windows 8.1 / Server 2012R2 & GOTO :NOTTESTEDWIN)
ver | findstr /i "10\.0\." > nul && (echo Windows 10 / Server 2016 & GOTO :NOTTESTEDWIN)
echo "Could not detect Windows version! exiting..."
color 4F & pause & exit /B 1
:NOTTESTEDWIN
echo "This is not a supported Windows version"
color 4F & pause & exit /B 1
:TESTEDWIN
REM put your code here
#13
0
The first line forces a WMI console installation if required on a new build. WMI always returns a consistent string of "Version=x.x.xxxx" so the token parsing is always the same for all Windows versions.
如果需要新建一个WMI控制台,第一行将强制安装。WMI总是返回一个一致的“Version=x.x”字符串。因此,所有Windows版本的令牌解析都是相同的。
Parsing from the VER output has variable text preceding the version info, making token positions random. Delimiters are only the '=' and '.' characters.
在版本信息之前,解析来自VER的输出有可变的文本,使得令牌的位置是随机的。分隔符只是'='和'。的字符。
The batch addition allows me to easily check versions as '510' (XP) up to '10000' for Win10. I don't use the Build value.
批量添加允许我可以轻松地检查版本为“510”(XP)到Win10的“10000”。我不使用构建值。
Setlocal EnableDelayedExpansion
wmic os get version /value 1>nul 2>nul
if %errorlevel% equ 0 (
for /F "tokens=2,3,4 delims==." %%A In ('wmic os get version /value') Do (
(Set /A "_MAJ=%%A")
(Set /A "_MIN=%%B")
(Set /A "_BLD=%%C")
)
(Set /A "_OSVERSION=!_MAJ!*100")
(Set /A "_OSVERSION+=!_MIN!*10")
)
endlocal
#1
11
Have you tried using the wmic
commands?
你试过使用wmic命令吗?
Try wmic os get version
试试wmic os得到版本。
This will give you the version number in a command line, then you just need to integrate into the batch file.
这将在命令行中给出版本号,然后只需要集成到批处理文件中。
#2
42
It's much easier (and faster) to get this information by only parsing the output of ver
:
通过解析ver的输出来获得这些信息要容易得多(而且更快)。
@echo off
setlocal
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version%" == "6.3" echo Windows 8.1
if "%version%" == "6.2" echo Windows 8.
if "%version%" == "6.1" echo Windows 7.
if "%version%" == "6.0" echo Windows Vista.
rem etc etc
endlocal
This table on MSDN documents which version number corresponds to which Windows product version (this is where you get the 6.1 means Windows 7 information from).
此表在MSDN文档中,版本号对应的是Windows产品版本(这是您从这里得到的6.1意味着Windows 7信息)。
The only drawback of this technique is that it cannot distinguish between the equivalent server and consumer versions of Windows.
这种技术的唯一缺点是它不能区分Windows的等效服务器和消费者版本。
#3
12
These one-line commands have been tested on Windows XP, Server 2012, 7 and 10 (thank you Mad Tom Vane).
这些单行命令已经在Windows XP、Server 2012、7和10上进行了测试(感谢Mad Tom Vane)。
Extract version x.y
in a cmd
console
for /f "tokens=4-7 delims=[.] " %i in ('ver') do @(if %i==Version (echo %j.%k) else (echo %i.%j))
Extract the full version x.y.z
for /f "tokens=4-7 delims=[.] " %i in ('ver') do @(if %i==Version (echo %j.%k.%l) else (echo %i.%j.%k))
In a batch script use %%
instead of single %
@echo off
for /f "tokens=4-7 delims=[.] " %%i in ('ver') do (if %%i==Version (set v=%%j.%%k) else (set v=%%i.%%j))
echo %v%
Version is not always consistent to brand name number
Be aware that the extracted version number does not always corresponds to the Windows name release. See below an extract from the full list of Microsoft Windows versions.
请注意,提取的版本号并不总是对应于Windows名称的发布。从微软Windows版本的完整列表中查看。
10.0
Windows 106.3
Windows Server 20126.3
Windows 8.1/!\
6.2
Windows 8/!\
6.1
Windows 7/!\
6.0
Windows Vista5.2
Windows XP x645.1
Windows XP5.0
Windows 20004.10
Windows 9810.0 Windows 10 6.3 Windows Server 2012 6.3 Windows 8.1 /!\ 6.2 Windows 8 /!\ 6.1 Windows 7 /!windowsvista 5.2 windowsxp x64 5.1 Windows XP 5.0视窗2000 4.10视窗98。
Please also up-vote answers from Agent Gibbs and peterbh as my answer is inspired from their ideas.
请从吉布斯和彼得·波黑的回答中得到答案,因为我的回答是来自他们的想法。
#4
5
I know it's an old question but I thought these were useful enough to put here for people searching.
我知道这是一个老问题,但我认为这些都很有用,可以供人们搜索。
This first one is a simple batch way to get the right version. You can find out if it is Server or Workstation (if that's important) in another process. I just didn't take time to add it.
第一个是简单的批处理方法,以获得正确的版本。在另一个过程中,您可以发现它是否是服务器或工作站(如果这很重要)。我只是没花时间把它加进去。
We use this structure inside code to ensure compliance with requirements. I'm sure there are many more graceful ways but this does always work.
我们在代码中使用这个结构来确保符合要求。我相信有很多更优雅的方式,但这总是有效的。
:: -------------------------------------
:: Check Windows Version
:: 5.0 = W2K
:: 5.1 = XP
:: 5.2 = Server 2K3
:: 6.0 = Vista or Server 2K8
:: 6.1 = Win7 or Server 2K8R2
:: 6.2 = Win8 or Server 2K12
:: 6.3 = Win8.1 or Server 2K12R2
:: 0.0 = Unknown or Unable to determine
:: --------------------------------------
echo OS Detection: Starting
ver | findstr /i "5\.0\."
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 2000
)
ver | findstr /i "5\.1\."
if %ERRORLEVEL% EQU 0 (
echo OS = Windows XP
)
ver | findstr /i "5\.2\."
if %ERRORLEVEL% EQU 0 (
echo OS = Server 2003
)
ver | findstr /i "6\.0\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Vista / Server 2008
)
ver | findstr /i "6\.1\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 7 / Server 2008R2
)
ver | findstr /i "6\.2\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 8 / Server 2012
)
ver | findstr /i "6\.3\." > nul
if %ERRORLEVEL% EQU 0 (
echo OS = Windows 8.1 / Server 2012R2
)
This second one isn't what was asked for but it may be useful for someone looking.
这第二个不是我们要的,但它可能对一个人有用。
Here is a VBscript function that provides version info, including if it is the Server (vs. workstation).
这是一个提供版本信息的VBscript函数,包括如果它是服务器(与工作站)。
private function GetOSVer()
dim strOsName: strOsName = ""
dim strOsVer: strOsVer = ""
dim strOsType: strOsType = ""
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOSes
strOsName = objOS.Caption
strOsVer = left(objOS.Version, 3)
Select Case strOsVer
case "5.0" 'Windows 2000
if instr(strOsName, "Server") then
strOsType = "W2K Server"
else
strOsType = "W2K Workstation"
end if
case "5.1" 'Windows XP 32bit
strOsType = "XP 32bit"
case "5.2" 'Windows 2003, 2003R2, XP 64bit
if instr(strOsName, "XP") then
strOsType = "XP 64bit"
elseif instr(strOsName, "R2") then
strOsType = "W2K3R2 Server"
else
strOsType = "W2K3 Server"
end if
case "6.0" 'Vista, Server 2008
if instr(strOsName, "Server") then
strOsType = "W2K8 Server"
else
strOsType = "Vista"
end if
case "6.1" 'Server 2008R2, Win7
if instr(strOsName, "Server") then
strOsType = "W2K8R2 Server"
else
strOsType = "Win7"
end if
case "6.2" 'Server 2012, Win8
if instr(strOsName, "Server") then
strOsType = "W2K12 Server"
else
strOsType = "Win8"
end if
case "6.3" 'Server 2012R2, Win8.1
if instr(strOsName, "Server") then
strOsType = "W2K12R2 Server"
else
strOsType = "Win8.1"
end if
case else 'Unknown OS
strOsType = "Unknown"
end select
Next
GetOSVer = strOsType
end Function 'GetOSVer
#5
3
This will return 5.1 for xp or 6/1 for windows 7
这将返回5.1的xp或6/1的windows 7。
for /f "tokens=4-7 delims=[.] " %%i in ('ver') do (
if %%i == Version set OSVersion=%%j.%%k
if %%i neq Version set OSVersion=%%i.%%j
)
#6
2
Here's my one liner to determine the windows version:
这是我的一个班轮来确定windows版本:
for /f "tokens=1-9" %%a in ('"systeminfo | find /i "OS Name""') do (set ver=%%e & echo %ver%)
This returns the windows version, i.e., XP, Vista, 7, and sets the value of "ver" to equal the same...
这将返回windows版本,即,XP, Vista, 7,并设置“ver”的值相等…
#7
2
Actually, that one liner doesn't work for windows xp since the ver contains xp in the string. Instead of 5.1 which you want, you would get [Version.5 because of the added token.
实际上,这一行代码并不适用于windows xp,因为它包含了字符串中的xp。而不是你想要的5.1,你会得到[版本。5因为添加了令牌。
I modified the command to look like this: for /f "tokens=4-6 delims=[. " %%i in ('ver') do set VERSION=%%i.%%j
我修改了命令如下:for /f“令牌=4-6 delims=[。“%%i in ('ver') do set VERSION=%%i.%% i.% i.%% %i %%。”
This will output Version.5 for xp systems which you can use to indentify said system inside a batch file. Sadly, this means the command cannot differentiate between 32bit
and 64bit
build since it doesn't read the .2 from 5.2 that denotes 64bit
XP.
这将输出版本。5用于xp系统,您可以使用该系统在批处理文件中识别该系统。遗憾的是,这意味着该命令不能区分32位和64位构建,因为它没有从5.2中读取.2,表示64位XP。
You could make it assign %%k
that token but doing so would make this script not detect windows vista, 7, or 8 properly as they have one token less in their ver string. Hope this helps!
您可以让它为标记指定%%k,但是这样做将使该脚本不能正确地检测windowsvista、7或8,因为它们的ver字符串中有一个标记更少。希望这可以帮助!
#8
2
Use a reg query and you will get an actual name: reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName
使用一个reg查询,你会得到一个实际的名字:reg查询“HKLM\软件\微软\Windows NT\CurrentVersion”/v ProductName。
This would return "Windows 7 Professional", "Windows Server 2008 R2 Standard", etc. You can then combine with the ver command to get exact version.
这将返回“Windows 7专业”,“Windows Server 2008 R2标准”,等等。然后你可以与ver命令结合,得到准确的版本。
#9
1
@echo off
for /f "tokens=2 delims=:" %%a in ('systeminfo ^| find "OS Name"') do set OS_Name=%%a
for /f "tokens=* delims= " %%a in ("%OS_Name%") do set OS_Name=%%a
for /f "tokens=3 delims= " %%a in ("%OS_Name%") do set OS_Name=%%a
if "%os_name%"=="XP" set version=XP
if "%os_name%"=="7" set version=7
This will grab the OS name as "7" or "XP"
这将抓取OS名称为“7”或“XP”
Then you can use this in a variable to do certain commands based on the version of windows.
然后,您可以在一个变量中使用它来执行基于windows版本的某些命令。
#10
1
If you are looking to simply show windows version in a batch file such as Windows 10 etc.
如果您想要简单地在批处理文件中显示windows版本,比如windows 10等等。
you can simply use the ver
command just type ver
and the windows version will be displayed
您可以简单地使用ver命令,只需键入ver,并且将显示windows版本。
#11
1
wmic os get Caption,CSDVersion /value
This will do the job.
这样做就行了。
#12
1
Here is another variant : some other solutions doesn't work with XP, this one does and was inspired by RLH solution.
这是另一种变体:一些其他的解决方案与XP不兼容,这一方法是由RLH解决方案所启发的。
This script will continue only if it detects the Windows version you want, in this example I want my script to run only in win 7, so to support other windows just change the GOTO :NOTTESTEDWIN
to GOTO :TESTEDWIN
这个脚本只会在它检测到你想要的Windows版本时才会继续,在这个例子中,我希望我的脚本只在win 7中运行,所以支持其他窗口只会改变GOTO:NOTTESTEDWIN:TESTEDWIN。
ver | findstr /i "5\.0\." && (echo Windows 2000 & GOTO :NOTTESTEDWIN)
ver | findstr /i "5\.1\." && (echo Windows XP 32bit & GOTO :NOTTESTEDWIN)
ver | findstr /i "5\.2\." && (echo Windows XP x64 / Windows Server 2003 & GOTO :NOTTESTEDWIN)
ver | findstr /i "6\.0\." > nul && (echo Windows Vista / Server 2008 & GOTO :NOTTESTEDWIN)
ver | findstr /i "6\.1\." > nul && (echo Windows 7 / Server 2008R2 & GOTO :TESTEDWIN)
ver | findstr /i "6\.2\." > nul && (echo Windows 8 / Server 2012 & GOTO :NOTTESTEDWIN)
ver | findstr /i "6\.3\." > nul && (echo Windows 8.1 / Server 2012R2 & GOTO :NOTTESTEDWIN)
ver | findstr /i "10\.0\." > nul && (echo Windows 10 / Server 2016 & GOTO :NOTTESTEDWIN)
echo "Could not detect Windows version! exiting..."
color 4F & pause & exit /B 1
:NOTTESTEDWIN
echo "This is not a supported Windows version"
color 4F & pause & exit /B 1
:TESTEDWIN
REM put your code here
#13
0
The first line forces a WMI console installation if required on a new build. WMI always returns a consistent string of "Version=x.x.xxxx" so the token parsing is always the same for all Windows versions.
如果需要新建一个WMI控制台,第一行将强制安装。WMI总是返回一个一致的“Version=x.x”字符串。因此,所有Windows版本的令牌解析都是相同的。
Parsing from the VER output has variable text preceding the version info, making token positions random. Delimiters are only the '=' and '.' characters.
在版本信息之前,解析来自VER的输出有可变的文本,使得令牌的位置是随机的。分隔符只是'='和'。的字符。
The batch addition allows me to easily check versions as '510' (XP) up to '10000' for Win10. I don't use the Build value.
批量添加允许我可以轻松地检查版本为“510”(XP)到Win10的“10000”。我不使用构建值。
Setlocal EnableDelayedExpansion
wmic os get version /value 1>nul 2>nul
if %errorlevel% equ 0 (
for /F "tokens=2,3,4 delims==." %%A In ('wmic os get version /value') Do (
(Set /A "_MAJ=%%A")
(Set /A "_MIN=%%B")
(Set /A "_BLD=%%C")
)
(Set /A "_OSVERSION=!_MAJ!*100")
(Set /A "_OSVERSION+=!_MIN!*10")
)
endlocal