在 PowerShell 中了解多種獲取 Windows 版本的方法

疑難排解您的系統並需要快速獲取您的Windows版本?幸運的是,您可以使用PowerShell來獲取您所使用的Windows版本。

檢查您使用的Windows版本非常重要,以便管理您的系統。在本教程中,您將學習到很多獲取Windows版本的方法和更多信息。

繼續閱讀並自行決定哪種方法最適合您!

前提條件

本教程將進行實際演示。要跟隨操作,請確保您有一個Windows系統。本教程使用帶有PowerShell 7的Windows 11。

使用PowerShell獲取Windows版本

在使用PowerShell進行複雜操作之前,從基礎知識開始,比如獲取系統信息。使用PowerShell,您可以通過systeminfo命令快速獲取Windows版本。

打開您的PowerShell,運行以下命令以獲取有關您的系統的詳細信息,包括操作系統(OS)版本。

systeminfo.exe

如下圖所示,您可以看到有關操作系統的詳細信息,包括當前版本號10.0.22622 N/A Build 22622

Getting the Windows version via the SystemInfo

選擇特定屬性以獲取Windows版本。太棒了!您剛剛檢索了詳細的系統信息。但是,如果您仔細看,命令很短,但輸出似乎很多。如果您只想要特定的屬性值,Get-ComputerInfo 命令是獲取特定系統信息(例如您的Windows版本)的最快方法之一。運行以下命令以獲取Windows版本、產品名稱、Windows內核版本以及您系統硬件的操作硬件抽象(OHA)。通過將 Select-Object 命令管道傳遞給 Get-ComputerInfo 命令,您可以只檢索選定的屬性。正如您所見,已安裝Windows 10專業版,其版本為2009。通過 System.Environment 類檢索Windows版本的另一種方法。此類提供對系統信息的訪問,例如OS版本、用戶名和其他環境變量。 相關文章:PowerShell環境變量:最終指南

Get-ComputerInfo | Select-Object OSName, OSVersion, OsHardwareAbstractionLayer
Getting the Windows version with the Get-ComputerInfo command

運行以下命令從System.Environment類別呼叫OSVersion.Version屬性。使用雙冒號(::)符號從類別呼叫靜態方法。

[System.Environment]::OSVersion.Version

如下所示,輸出顯示作業系統版本資訊:

Property Value Description
Major 10 Despite saying 10, this may indicate Windows 10 or 11 as both use the same Major version.
Minor 0 There are two types of Windows releases, major and minor. Major releases are the “big” updates like the Creator update, and minor releases are smaller cumulative updates.
Build 22622 The number used to check the Windows version. In this case, it is for version 22H2.
Revision 0 Denotes a sub-version of the build.
Getting the windows via the System.Environment class

挖掘特定屬性以取得Windows版本

PowerShell cmdlets提供有關系統的一些資訊。但如果您只打算獲取您的Windows版本,則Get-ItemProperty cmdlet是另一個選項。此cmdlet命令可讓您存取註冊表並從中獲取各種屬性。

運行以下命令以存取註冊表(Get-ItemProperty)並擷取當前Windows版本的DisplayVersion(HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion)。顯示版本(ReleaseID已棄用)是每個Windows版本的唯一識別碼。

(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").DisplayVersion

下面的命令擷取並列印當前的Windows版本,即22H2。

Getting the Windows version via the Get-ItemProperty command

查詢當前的Windows版本

您已經看到如何存取屬性以獲取當前的Windows版本。而當您需要處理屬性時,請熟悉Get-CimInstance cmdlet。

Get-CimInstance cmdlet 是通过查询 Windows Management Instrumentation (WMI) 对象来获取 Windows 版本的另一种方式。通过这样做,您可以访问系统硬件的各种属性。

运行以下命令来查询 (Get-CimInstance) Win32_OperatingSystem 类(WMI 对象),以获取当前 Windows .versionWin32_OperatingSystem 类提供有关操作系统的信息。

(Get-CimInstance Win32_OperatingSystem) | Select-Object Caption, Version

如下所示,此输出类似于使用 System.Environment 类。但这次,您仅获得实际的操作系统版本,而无需属性标题。由于 WMI 对象中不提供修订版本,因此修订版本也被省略。

Querying WMI for the current Windows version

结论

在本教程中,您已经学会了在 PowerShell 中获取当前 Windows 版本的众多方法。在 PowerShell 中检查 Windows 版本是一项有价值的技能。无论您是在解决系统问题还是检查应用程序与当前操作系统的兼容性,此技能都可能派上用场。

现在,为什么不为检查 Windows 版本创建一个脚本呢?也许安排一个任务,在特定时间运行该脚本?

Source:
https://adamtheautomator.com/powershell-to-get-the-windows-version/