如何找到我正在使用的 Windows 版本?
我正在使用 PowerShell 2.0 并尝试过:
PS C:\> ver
The term 'ver' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify tha
t the path is correct and try again.
At line:1 char:4
+ ver <<<<
+ CategoryInfo : ObjectNotFound: (ver:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
我该怎么做呢?
由于您可以访问 .NET 库,因此您可以访问 System.Environment
类的 OSVersion
属性来获取此信息。对于版本号,有 Version
属性。
例如,
PS C:\> [System.Environment]::OSVersion.Version
Major Minor Build Revision
----- ----- ----- --------
6 1 7601 65536
可以在 here 中找到 Windows 版本的详细信息。
要获取 Windows 版本号,正如 Jeff 在他的回答中指出的那样,使用:[Environment]::OSVersion 值得注意的是,结果是 [System.Version] 类型,因此可以检查 Windows 7 /Windows Server 2008 R2 及更高版本带有 [Environment]::OSVersion.Version -ge (new-object 'Version' 6,1) 但是这不会告诉您它是客户端还是服务器 Windows,也不会告诉您版本的名称。使用 WMI 的 Win32_OperatingSystem 类(始终为单个实例),例如:(Get-WmiObject -class Win32_OperatingSystem)。Caption 将返回类似于 Microsoft® Windows Server® 2008 Standard 的内容
不幸的是,大多数其他答案都没有提供特定于 Windows 10 的信息。
Windows 10 有自己的 versions:1507, 1511, 1607, 1703, etc。这就是 winver
显示的内容。
Powershell:
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId
Command prompt (CMD.EXE):
Reg Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ReleaseId
另见related question on superuser。
至于其他 Windows 版本,请使用 systeminfo
。 Powershell 包装器:
PS C:\> systeminfo /fo csv | ConvertFrom-Csv | select OS*, System*, Hotfix* | Format-List
OS Name : Microsoft Windows 7 Enterprise
OS Version : 6.1.7601 Service Pack 1 Build 7601
OS Manufacturer : Microsoft Corporation
OS Configuration : Standalone Workstation
OS Build Type : Multiprocessor Free
System Type : x64-based PC
System Locale : ru;Russian
Hotfix(s) : 274 Hotfix(s) Installed.,[01]: KB2849697,[02]: KB2849697,[03]:...
同一命令的 Windows 10 输出:
OS Name : Microsoft Windows 10 Enterprise N 2016 LTSB
OS Version : 10.0.14393 N/A Build 14393
OS Manufacturer : Microsoft Corporation
OS Configuration : Standalone Workstation
OS Build Type : Multiprocessor Free
System Type : x64-based PC
System Directory : C:\Windows\system32
System Locale : en-us;English (United States)
Hotfix(s) : N/A
winver
,在服务器上很容易记住 systeminfo
。多年来,我一直感到困惑的是,没有统一的方式来获取这些信息。
OS Version : 6.3.9600 N/A Build 9600
。因此,在 W81 以下的版本中,查看(总是被忽视的)LTSB 版本可能会提供更多信息。查看来自:(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx
的输出,可能类似于:9600.19179.amd64fre.winblue_ltsb_escrow.181015-1847
。我的猜测是 181015
部分是构建日期,而 1847
是构建或发布版本。您可能还需要将其与 kernel, hal 进行比较。
meta
问题吗?
BuildLabEx
就是它,但实际上它仍然报告一年前的(RTM?)构建 22000.1 + 实验室字符串,而不是根据 winver 报告我当前的构建 22000.675。 CurrentVersion
键中的任何内容都不会将我当前的内部版本号报告为完整字符串的一部分。只有值 UBR
将报告正确的整数 675,但不是以完整构建字符串的形式。
Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer
返回
WindowsProductName WindowsVersion OsHardwareAbstractionLayer
------------------ -------------- --------------------------
Windows 10 Enterprise 1709 10.0.16299.371
与上述所有解决方案不同,这将为您提供完整版本的 Windows(包括修订/内部版本号):
(Get-ItemProperty -Path c:\windows\system32\hal.dll).VersionInfo.FileVersion
结果:
10.0.10240.16392 (th1_st1.150716-1608)
10.0.10586.0 (th2_release.151029-1700)
,而 winload.exe 有 10.0.10586.63 (th2_release.160104-1513)
。
version
。操作系统是按部分构建(和更新)的,即内核、HAL、UBR 和 功能 等。所以我们应该真正显示所有这些。在这方面,我认为 BuildLabEx
、Kernel
和 HAL
(按此顺序)将是提供更合适的 版本 的最合适方式。但既然你似乎知道什么是错,你应该发布什么是正确。
从 PowerShell 5 开始:
Get-ComputerInfo
Get-ComputerInfo -Property Windows*
我认为这个命令几乎尝试了迄今为止发现的 1001 种不同的方法来收集系统信息......
如果要区分 Windows 8.1 (6.3.9600) 和 Windows 8 (6.2.9200),请使用
(Get-CimInstance Win32_OperatingSystem).Version
获得正确的版本。 [Environment]::OSVersion
在 Windows 8.1 中无法正常工作(它返回 Windows 8 版本)。
(Get-CimInstance Win32_OperatingSystem).Version
和 [Environment]::OSVersion
都适用于我并返回相同的结果:6.3.9600.0
我正在提炼其中一个answers
我在尝试匹配 winver.exe 的输出时遇到了这个问题:
Version 1607 (OS Build 14393.351)
我能够使用以下方法提取构建字符串:
,((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name BuildLabEx).BuildLabEx -split '\.') | % { $_[0..1] -join '.' }
结果:14393.351
更新:这是一个使用正则表达式的稍微简化的脚本
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx -match '^[0-9]+\.[0-9]+' | % { $matches.Values }
如果您试图破译 MS 放在他们的补丁站点上的信息,例如 https://technet.microsoft.com/en-us/library/security/ms17-010.aspx
您将需要一个组合,例如:
$name=(Get-WmiObject Win32_OperatingSystem).caption $bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture $ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId Write-Host $name, $bit, $ver
微软 Windows 10 家庭版 64 位 1703
我采用了上面的脚本并对其进行了一些调整以得出以下结论:
$name=(Get-WmiObject Win32_OperatingSystem).caption
$bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture
$vert = " Version:"
$ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId
$buildt = " Build:"
$build= (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx -match '^[0-9]+\.[0-9]+' | % { $matches.Values }
$installd = Get-ComputerInfo -Property WindowsInstallDateFromRegistry
Write-host $installd
Write-Host $name, $bit, $vert, $ver, `enter code here`$buildt, $build, $installd
要得到这样的结果:
Microsoft Windows 10 家庭 64 位版本:1709 内部版本:16299.431 @{WindowsInstallDateFromRegistry=18-01-01 2:29:11 AM}
提示:我会很感激从安装日期中删除前缀文本,以便我可以用更易读的标题替换它。
[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970')).AddSeconds($(get-itemproperty "HKLM:\Software\Microsoft\Windows NT\CurrentVersion").InstallDate)
它稍微复杂一些,但运行起来要快得多。您甚至可以省略时区部分:([datetime]'1/1/1970').AddSeconds($(get-itemproperty "HKLM:\Software\Microsoft\Windows NT\CurrentVersion").InstallDate)
要在 Windows 10 1809 上的 PowerShell v5 中生成与 winver.exe 相同的输出:
$Version = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\'
"Version $($Version.ReleaseId) (OS Build $($Version.CurrentBuildNumber).$($Version.UBR))"
Version 20H2 (OS Build 19042.804)
,但这给出了 Version 2009 (OS Build 19042.804)
。将 ReleaseId
更改为 DisplayVersion
可以解决此问题,但旧操作系统没有 DisplayVersion
。
正如 MoonStom 所说,[Environment]::OSVersion
在升级的 Windows 8.1 上无法正常工作(它返回 Windows 8 版本):link。
如果您想区分 Windows 8.1 (6.3.9600) 和 Windows 8 (6.2.9200),您可以使用 (Get-CimInstance Win32_OperatingSystem).Version
来获取正确的版本。但是,这在 PowerShell 2 中不起作用。所以使用这个:
$version = $null
try {
$version = (Get-CimInstance Win32_OperatingSystem).Version
}
catch {
$version = [System.Environment]::OSVersion.Version | % {"{0}.{1}.{2}" -f $_.Major,$_.Minor,$_.Build}
}
利用:
Get-WmiObject -class win32_operatingsystem -computer computername | Select-Object Caption
应该像这样容易:
Get-ComputerInfo | select windowsversion
这确实是一个很长的话题,可能是因为答案虽然正确,但并没有解决基本问题。我偶然发现了这个网站:Version & Build Numbers,它清楚地概述了 Microsoft Windows 世界的内容。
因为我的兴趣是知道我正在处理哪个确切的 Windows 操作系统,所以我将整个版本的彩虹放在一边,而是专注于 BuildNumber。可以通过以下方式获得内部版本号:
([Environment]::OSVersion.Version).Build
或通过:
(Get-CimInstance Win32_OperatingSystem).buildNumber
选择是您的选择,无论您喜欢哪种方式。所以从那里我可以做一些事情:
switch ((Get-CimInstance Win32_OperatingSystem).BuildNumber)
{
6001 {$OS = "W2K8"}
7600 {$OS = "W2K8R2"}
7601 {$OS = "W2K8R2SP1"}
9200 {$OS = "W2K12"}
9600 {$OS = "W2K12R2"}
14393 {$OS = "W2K16v1607"}
16229 {$OS = "W2K16v1709"}
default { $OS = "Not Listed"}
}
Write-Host "Server system: $OS" -foregroundcolor Green
注意:如您所见,我将上述内容仅用于服务器系统,但它可以轻松应用于工作站,甚至可以巧妙地扩展以支持两者......但我将把它留给你。
享受,玩得开心!
""+[Environment]::OSVersion.Version.Major+"."+[Environment]::OSVersion.Version.Minor
您有包含 Microsoft 官方构建表的链接吗?
Windows PowerShell 2.0:
$windows = New-Object -Type PSObject |
Add-Member -MemberType NoteProperty -Name Caption -Value (Get-WmiObject -Class Win32_OperatingSystem).Caption -PassThru |
Add-Member -MemberType NoteProperty -Name Version -Value [Environment]::OSVersion.Version -PassThru
Windows PowerShell 3.0:
$windows = [PSCustomObject]@{
Caption = (Get-WmiObject -Class Win32_OperatingSystem).Caption
Version = [Environment]::OSVersion.Version
}
对于显示(两个版本):
"{0} ({1})" -f $windows.Caption, $windows.Version
你们太努力了。这适用于使用 Enter-PSSession 的本地或远程会话 - 试一试。
您所要做的就是输入:
cmd ?
Microsoft Windows [版本 10.0.19042.1237]
除了其他答案之外,这里还有一些可以使用 PowerShell 检索的有用信息:
通过 PowerShell 查询操作系统和硬件信息:
查询通用操作系统(操作系统)信息:
查看操作系统名称的最快方法:
cmd ?
#使用获取计算机信息:
Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer
#使用Get-WmiObject:
$name=(Get-WmiObject Win32_OperatingSystem).caption
$bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture
$ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId
Write-Host " OS-Name: `t $name `n Architct: `t $bit `n Release: `t $ver"
列出主要次要版本信息:
[System.Environment]::OSVersion.Version
查询主机名:
$Env:ComputerName
或者
hostname #cmd command
此外,如果您知道 IP 地址,请使用“ping”命令(例如:ping /a <your_ip_address>
),您将在第一行看到您的“主机名”。
查询当前(登录)用户:
whoami #cmd command
或者
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
查询映射驱动器:列出映射驱动器 - 使用 WMI:
Get-WmiObject -Class Win32_LogicalDisk | Format-Table
或者
wmic logicaldisk get name #list just logical-drive letters
或者,列出逻辑驱动器信息:FreeSpace、Provider(真实网络位置)、Size 和 VolumeName:
wmic logicaldisk list brief
列出映射驱动器 - 使用 [DriveInfo] 类:
[System.IO.DriveInfo]::GetDrives()
列出可移动驱动器:
$drives = [System.IO.DriveInfo]::GetDrives()
$r = $drives | Where-Object { $_.DriveType -eq 'Removable' -and $_.IsReady }
if ($r) {
return @($r)[-1]
}
查询磁盘容量、空间和卷类型
Invoke-Command -ComputerName S1 {Get-PSDrive C} | Select-Object PSComputerName,Used,Free
可用空间:
(Get-PSDrive C).Free
或(以 GB 为单位)
[Math]::Floor(((Get-PSDrive C).Free /[Math]::Pow(2, 30)*10)) /10
已用空间:
(Get-PSDrive C).Used
或(以 GB 为单位的已用空间)
[Math]::Floor(((Get-PSDrive C).Used /[Math]::Pow(2, 30)*10)) /10
另外要查看总空间:(以 GB 为单位)
$totalSpace = ((Get-PSDrive C).Used + (Get-PSDrive C).Free)/(1024*1024*1024)
OR
$totalSpace = ((Get-PSDrive C).Used + (Get-PSDrive C).Free)/[Math]::Pow(2, 30)
四舍五入的值:
[Math]::Floor($totalSpace*10) / 10
OR
[Math]::Round($totalSpace,1)
查询主板信息:
wmic baseboard get product,Manufacturer,version,serialnumber
查询磁盘卷(磁盘分区)信息:Get-Volume 返回有关存储驱动器分区的信息,例如:
Get-Volume # All partitions
Get-Volume -DriveLetter C # Specific partition
#文件系统类型:
Get-Volume -DriveLetter C | select FileSystem
(Get-Volume -DriveLetter C).FileSystem
#分区大小:
Get-Volume -DriveLetter C | select Size
OR (in GB)
[Math]::Floor(((Get-Volume -DriveLetter C).Size/[Math]::Pow(2, 30)*10)) /10
查询内存/查询 RAM
Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum
OR (in GB)
$memory = (Get-WmiObject Win32_PhysicalMemory | Measure -Property Capacity -Sum).Sum
$memory = [Math]::Floor(($memory/[Math]::Pow(2, 30)*10)) /10
$memory.ToString() + " gb"
#Query RAM,包括频率/速度:
Get-CimInstance win32_physicalmemory | Format-Table Manufacturer,Banklabel,Configuredclockspeed,Devicelocator,Capacity,Serialnumber –autosize
如前所述,这个答案有点超出了所提出的问题,但对于那些想要使用 PowerShell 获得更多操作系统或硬件信息的人来说可能很有用。
[System.Environment]::OSVersion.Version
是我最喜欢的,适用于在 mac Monterey 上与 brew 一起安装的 pwsh
。
(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name BuildLabEx).BuildLabEx
(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Client.OS.rs2.amd64').version
根据 Tim 之前的回答,这个特定位置的好处是该属性已经处于我所说的首选格式。
相当于 winver 的 Powershell
适用于 20h2 之前的所有 Windows 10 版本,速度快且不太复杂*
function Get-WinVer() {
$win_release = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").displayversion
if (!($win_release)) {
$win_release = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId}
$win_release
}
Get-WinVer
它准确地显示了 winver.exe
在“版本”旁边显示的内容。
我没想到要读这么多才能得出这段代码,我真的希望我不必为 22h1 调整它(或者当时的名字是什么)。
*:微软确实让它变得比它应该的更复杂
与所有其他解决方案(在 Windows 10 上测试)不同,这将为您提供 Windows 的完整和正确(与您在运行 winver.exe 时找到的相同版本号)版本(包括修订/内部版本号):
Function Get-OSVersion {
Param($ComputerName)
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
$all = @()
(Get-Childitem c:\windows\system32) | ? Length | Foreach {
$all += (Get-ItemProperty -Path $_.FullName).VersionInfo.Productversion
}
$version = [System.Environment]::OSVersion.Version
$osversion = "$($version.major).0.$($version.build)"
$minor = @()
$all | ? {$_ -like "$osversion*"} | Foreach {
$minor += [int]($_ -replace".*\.")
}
$minor = $minor | sort | Select -Last 1
return "$osversion.$minor"
}
}
我搜索了很多以找出确切的版本,因为 WSUS 服务器显示错误的版本。最好的方法是从 UBR 注册表 KEY 中获取修订。
$WinVer = New-Object –TypeName PSObject
$WinVer | Add-Member –MemberType NoteProperty –Name Major –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' CurrentMajorVersionNumber).CurrentMajorVersionNumber
$WinVer | Add-Member –MemberType NoteProperty –Name Minor –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' CurrentMinorVersionNumber).CurrentMinorVersionNumber
$WinVer | Add-Member –MemberType NoteProperty –Name Build –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' CurrentBuild).CurrentBuild
$WinVer | Add-Member –MemberType NoteProperty –Name Revision –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' UBR).UBR
$WinVer
使用Windows Powershell,可以通过以下方式获取你需要的数据
标题:
(Get-WmiObject -class Win32_OperatingSystem).Caption
发布 ID:
(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
版本:
(Get-CimInstance Win32_OperatingSystem).version
[解决了]
#copy all the code below:
#save file as .ps1 run and see the magic
Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption
(Get-CimInstance Win32_OperatingSystem).version
#-------------comment-------------#
#-----finding windows version-----#
$version= (Get-CimInstance Win32_OperatingSystem).version
$length= $version.Length
$index= $version.IndexOf(".")
[int]$windows= $version.Remove($index,$length-2)
$windows
#-----------end------------------#
#-----------comment-----------------#
您也可以通过检查 OSVersion.Version.Major 来使用类似的东西:
IF ([System.Environment]::OSVersion.Version.Major -ge 10) {Write-Host "Windows 10 or above"}
IF ([System.Environment]::OSVersion.Version.Major -lt 10) {Write-Host "Windows 8.1 or below"}
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Client.OS.rs2.amd64\Version '对于 Win 10 客户端'
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Server.OS.amd64\Version '对于服务器操作系统'
我只想完成一个小脚本。我们使用了之前回答过的switch版本,只是详细说明了。没有地方会给你我们习惯的友好名称。 Windows 10 1909 或 Windows 10 20H2。所以我们必须手动对它们进行编程。
$osversion = (Get-CimInstance -class Win32_OperatingSystem).Caption
$buildnumber = (Get-CimInstance Win32_OperatingSystem).BuildNumber
if($osversion -match "Windows 10")
{
switch ($buildnumber)
{
10240 {$OS = "Windows 10 1507"}
10586 {$OS = "Windows 10 1511"}
14393 {$OS = "Windows 10 1607"}
15063 {$OS = "Windows 10 1703"}
16299 {$OS = "Windows 10 1709"}
17134 {$OS = "Windows 10 1803"}
17763 {$OS = "Windows 10 1809"}
18362 {$OS = "Windows 10 1903"}
18363 {$OS = "Windows 10 1909"}
19041 {$OS = "Windows 10 20H1"}
19042 {$OS = "Windows 10 20H2"}
19043 {$OS = "Windows 10 21H1"}
default { $OS = "Not Listed"}
}
}
if($osversion -match "Windows Server")
{
switch ($buildnumber)
{
3790 {$OS = "Windows Server 2003 R2"}
6001 {$OS = "Windows Server 2008"}
7600 {$OS = "Windows Server 2008 SP1"}
7601 {$OS = "Windows Server 2008 R2"}
9200 {$OS = "Windows Server 2012"}
9600 {$OS = "Windows Server 2012 R2"}
14393 {$OS = "Windows Server 2016"}
17763 {$OS = "Windows Server 2019"}
}
}
Write-Host "Server system: $OS | $osversion | $buildnumber" -foregroundcolor Green
现在,如果您想像我想使用 invoke-command
或 new-pssession
一样一次扫描多台电脑,请注意 Get-WMIObject
已贬值并替换为 get-ciminstance
如果您想要一个示例,我可以稍后提供 如果您使用的是 windows 2003 R2 或更早版本.. 停止移动到新的操作系统。
powershell 中的 C:\ 提示符或 cmd 提示符窗口中的 systeminfo 提供操作系统名称版本配置制造商等等...
$OSVersion = [Version](Get-ItemProperty -Path "$($Env:Windir)\System32\hal.dll" -ErrorAction SilentlyContinue).VersionInfo.FileVersion.Split()[0]
在 Windows 10 上返回:10.0.10586.420
然后,您可以使用该变量访问属性以进行细粒度比较
$OSVersion.Major equals 10
$OSVersion.Minor equals 0
$OSVersion.Build equals 10586
$OSVersion.Revision equals 420
此外,您可以使用以下方法比较操作系统版本
If ([Version]$OSVersion -ge [Version]"6.1")
{
#Do Something
}
不定期副业成功案例分享
[Environment]::OSVersion
在 windows-10 中有效,OSVersion.Version.Major
返回 10。winver
时,它显示版本 1607。但上面的 powershell 命令没有给出 1607。我在 Powershell 中哪里可以得到这个“1607”数字?(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
[System.Environment]::OSVersion
未被弃用,它在后台使用的方法已被弃用。新的 PS 版本正在改变后端行为:github.com/PowerShell/PowerShell/issues/…