如何在 PowerShell 中获取 Windows 版本

  1. 在 PowerShell 中使用 [System.Environment] 类获取 Windows 版本
  2. 在 PowerShell 中使用 Get-ComputerInfo cmdlet 获取 Windows 版本
  3. 使用带有 Get-WMIObject cmdlet 的 WMI 类在 PowerShell 中获取 Windows 版本
  4. 使用 systeminfo 传统命令 PowerShell 获取 Windows 版本
  5. 使用 Get-CimInstance 传统命令 PowerShell 获取 Windows 版本
  6. 结论
如何在 PowerShell 中获取 Windows 版本

获取您计算机上运行的 Windows 操作系统的最快方法是使用 winver 命令。在 Windows PowerShell 中,有多种方法可以获取您的 Windows 版本操作系统,我们将在本文中讨论这些方法。

有多种方法可以实现这一点,每种方法都有其优缺点和使用案例。在本文中,我们将探索五种不同的方法来获取 PowerShell 中的 Windows 版本。

每种方法都为 Windows 环境提供了独特的见解,为管理员和脚本编写者提供了多功能的工具,以收集重要的系统信息。

在 PowerShell 中使用 [System.Environment] 类获取 Windows 版本

在 PowerShell 中,您可以使用 [Environment]::OSVersion.Version 方法检索有关 Windows 版本的详细信息。此方法访问 Environment 类中的静态 Version 属性,该属性提供访问操作系统版本的简单方法。

示例代码:

[System.Environment]::OSVersion.Version

当我们使用 [Environment]::OSVersion.Version 时,我们正在访问 Environment 类的 OSVersion 属性,该属性提供有关操作系统环境的信息。具体来说,我们正在检索该对象的 Version 属性,该属性包含有关 Windows 版本的详细信息。

此方法返回一个 System.Version 对象,该对象以主要和次要版本号的组合表示操作系统的版本。通过访问此对象的 MajorMinor 属性,我们可以提取这些版本号并在 PowerShell 脚本中按需使用。

输出:

在 PowerShell 中查找 Windows 版本 - 输出 1

我们可以参考官方 Microsoft 文档来交叉参考您当前运行的 Windows 版本操作系统。

然而,如果您使用的是最新的操作系统,如 Windows 11 或 Windows Server 2019,它将不会显示正确的版本,因为它仍会显示一个主要构建 10,代表 Windows 10 和 Windows Server 2016。因此,上述命令仅在您运行 Windows 10 和 Windows Server 2016 时才会显示正确的值。

在 PowerShell 中使用 Get-ComputerInfo cmdlet 获取 Windows 版本

在 PowerShell 中,您可以使用 Get-ComputerInfo cmdlet 轻松检索有关 Windows 操作系统的详细信息。此 cmdlet 收集有关本地计算机系统的信息,包括操作系统名称、版本和硬件抽象层(HAL)。

示例代码:

Get-ComputerInfo | Select-Object OSName, OSVersion, OsHardwareAbstractionLayer

当我们使用 Get-ComputerInfo cmdlet 后接 Select-Object 时,我们正在检索有关本地计算机系统的综合信息。通过指定 OSNameOSVersionOsHardwareAbstractionLayer 属性,我们选择有关操作系统的特定详细信息,例如其名称、版本和硬件抽象层(HAL)。

此方法使我们能够收集有关 Windows 环境的详细信息,这对于各种管理任务、故障排除或脚本目的非常有用。通过访问和显示这些属性,我们可以深入了解 Windows 系统的配置和规格,有助于系统管理和维护。

输出:

在 PowerShell 中查找 Windows 版本 - 输出 2

使用带有 Get-WMIObject cmdlet 的 WMI 类在 PowerShell 中获取 Windows 版本

我们还可以使用 Windows 管理工具(WMI)类检查当前操作系统的版本。

示例代码:

(Get-WmiObject -class Win32_OperatingSystem).Caption

当我们执行 (Get-WmiObject -class Win32_OperatingSystem).Caption 时,我们正在利用 Get-WmiObject cmdlet 查询 Windows 管理工具(WMI)以获取有关操作系统的信息。具体来说,我们正在定位 Win32_OperatingSystem 类,该类包含有关操作系统的详细信息。

通过访问结果对象的 .Caption 属性,我们正在检索操作系统的名称。此方法提供了通过 PowerShell 直接获取 Windows 版本信息的简单方法,方便用于各种脚本和管理任务。

输出:

在 PowerShell 中查找 Windows 版本 - 输出 3

[System.Environment] 类和 Get-ComputerInfo cmdlet 不同,WMI 对象在您使用最新版本时能正确显示 Windows 操作系统版本。

使用 systeminfo 传统命令 PowerShell 获取 Windows 版本

我们还可以使用带有 Windows PowerShell cmdlet 包装器的 systeminfo 传统命令输出详细的操作系统版本。通过将 systeminfo 与 PowerShell cmdlet 结合使用,您可以提取有关 Windows 版本的特定信息。

systeminfo /fo csv | ConvertFrom-Csv | select OS*, System*, Hotfix* | Format-List

当我们执行命令 systeminfo /fo csv 时,我们正在利用 systeminfo 命令行工具以 CSV 格式收集详细的系统信息。

然后,我们使用 ConvertFrom-Csv 将 CSV 格式的输出转换为 PowerShell 对象。通过将结果传递给 Select,我们筛选出我们感兴趣的属性,特别是以 OSSystemHotfix 开头的属性。

最后,我们应用 Format-List 以格式化列表视图呈现信息。

输出:

在 PowerShell 中查找 Windows 版本 - 输出 4

使用 Get-CimInstance 传统命令 PowerShell 获取 Windows 版本

此 cmdlet 是 PowerShell 中公共信息模型(CIM)基础结构的一部分,使您能够以标准化的方式查询系统信息。通过定位 Win32_OperatingSystem 类,您可以访问诸如操作系统名称和版本等属性。

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

当我们执行 (Get-CimInstance Win32_OperatingSystem) | Select-Object Caption, Version 时,我们正在利用 Get-CimInstance cmdlet 从 Win32_OperatingSystem 类中检索有关 Windows 操作系统的信息。该类表示操作系统的各种属性。通过将结果传递给 Select-Object,我们指定我们感兴趣的属性,即 Caption(代表操作系统的名称)和 Version

输出:

在 PowerShell 中查找 Windows 版本 - 输出 5

结论

在 PowerShell 中,获取 Windows 版本对于系统管理和脚本任务至关重要。我们已经探索了五种不同的方法来实现这一目的:使用 [System.Environment] 类、Get-ComputerInfo cmdlet、Get-WmiObject cmdlet、systeminfo 命令和 Get-CimInstance cmdlet。

每种方法都提供了独特的优势,可能会根据特定的要求和场景而有所偏好。无论是通过 [System.Environment] 类直接访问版本,还是使用 Get-ComputerInfoGet-CimInstance 查询详细的系统信息,PowerShell 都提供了强大的工具,用于有效管理和监控 Windows 环境。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn