在批处理文件中获取 Windows 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13212033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Windows version in a batch file
提问by Yerko Antonio
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?
有没有其他方法可以做我想做的事?
采纳答案by liamog
Have you tried using the wmic
commands?
您是否尝试过使用wmic
命令?
Try
wmic os get version
尝试
wmic os get version
This will give you the version number in a command line, then you just need to integrate into the batch file.
这将在命令行中为您提供版本号,然后您只需要集成到批处理文件中。
回答by Jon
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%" == "10.0" echo Windows 10
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 tableon 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。
回答by olibre
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
x.y
在cmd
控制台中提取版本
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
提取完整版 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 名称版本。请参阅以下Microsoft 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 98
10.0
Windows 106.3
Windows Server 20126.3
Windows 8.1 Windows 8 Windows 7 Windows Vista Windows XP x64 Windows XP Windows 2000 Windows 98/!\
6.2
/!\
6.1
/!\
6.0
5.2
5.1
5.0
4.10
Please also up-vote answers from Agent Gibbsand peterbhas my answer is inspired from their ideas.
回答by RLH
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
回答by peterbh
This will return 5.1 for xp or 6/1 for windows 7
这将为 xp 返回 5.1 或为 Windows 7 返回 6/1
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
)
回答by Badr Elmers
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
将GOTO :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
回答by Badr Elmers
So the answers I read here are mostly lengthy. The shortest answer by Alex works good, but not so clean.
所以我在这里读到的答案大多很长。亚历克斯最短的答案效果很好,但不是那么干净。
Best part is that unlike others', it works fast.
最好的部分是,与其他人不同的是,它工作得很快。
I came up with my own one-liner:
我想出了我自己的单线:
FOR /F "usebackq tokens=3,4,5" %i IN (`REG query "hklm\software\microsoft\windows NT\CurrentVersion" /v ProductName`) DO echo %i %j %k
Above is for command prompt. To make it work in batch file, replace all %with %%to make it look like:
以上是命令提示符。要使其在批处理文件中工作,请将所有%替换为%%以使其看起来像:
FOR /F "usebackq tokens=3,4,5" %%i IN (`REG query "hklm\software\microsoft\windows NT\CurrentVersion" /v ProductName`) DO echo %%i %%j %%k
In my computer, it simply produces Windows 10 Pro
在我的电脑中,它只是生成Windows 10 Pro
回答by Mike Swanson
Use a reg query and you will get an actual name: reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName
使用 reg 查询,您将获得一个实际名称:reg query "HKLM\SOFTWARE\Microsoft\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 Professional”、“Windows Server 2008 R2 Standard”等。然后您可以结合 ver 命令来获取确切版本。
回答by user1
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”的值设置为相同...
回答by Agent Gibbs
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.
实际上,由于 ver 在字符串中包含 xp,因此该衬垫不适用于 windows xp。由于添加了令牌,您将获得 [Version.5] 而不是您想要的 5.1。
I modified the command to look like this:
for /f "tokens=4-6 delims=[. " %%i in ('ver') do set VERSION=%%i.%%j
我将命令修改为如下所示:
for /f "tokens=4-6 delims=[. " %%i in ('ver') do set VERSION=%%i.%%j
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.
这将为 xp 系统输出 Version.5,您可以使用它在批处理文件中识别所述系统。可悲的是,这意味着该命令无法区分32bit
和64bit
构建,因为它不读取表示64bit
XP 的5.2 中的 .2 。
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
该标记,但这样做会使该脚本无法正确检测 Windows Vista、7 或 8,因为它们的 ver 字符串中少了一个标记。希望这可以帮助!