【问题标题】:How to find the Windows version from the PowerShell command line如何从 PowerShell 命令行查找 Windows 版本
【发布时间】:2011-11-11 21:52:42
【问题描述】:

如何找到我正在使用的 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

我该怎么做?

【问题讨论】:

  • 如果您在 2019 年及以后查看此内容,请忽略被标记为的答案并直接访问the one that is correct。不客气。

标签: windows powershell powershell-2.0


【解决方案1】:

由于您可以访问 .NET 库,因此您可以访问 System.Environment 类的 OSVersion 属性来获取此信息。对于版本号,有Version 属性。

例如,

PS C:\> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
6      1      7601   65536

Windows 版本详情请见here。

【讨论】:

  • 请注意[Environment]::OSVersion 在windows-10 中有效,OSVersion.Version.Major 返回 10。
  • 当我运行 winver 时,它会显示版本 1607。但上面的 powershell 命令没有给出 1607。我在 Powershell 中哪里可以得到这个“1607”数字?
  • @CMCDragonkai (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
  • 从 Windows 8.1 开始不推荐使用此方法。有关详细信息,请参阅此link。
  • @SlogmeisterExtraordinaire 命令[System.Environment]::OSVersion 未被弃用,它在后台使用的方法已被弃用。新的 PS 版本正在改变后端行为:github.com/PowerShell/PowerShell/issues/…
【解决方案2】:
  1. 要获取 Windows 版本号,正如 Jeff 在他的 answer 中所说,使用:

    [Environment]::OSVersion
    

    值得注意的是,结果是 [System.Version] 类型,因此可以检查 Windows 7/Windows Server 2008 R2 及更高版本

    [Environment]::OSVersion.Version -ge (new-object 'Version' 6,1)
    

    但是这不会告诉你它是客户端还是服务器 Windows,也不会告诉你版本的名称。

  2. 使用 WMI 的 Win32_OperatingSystem 类(总是单实例),例如:

    (Get-WmiObject -class Win32_OperatingSystem).Caption
    

    会返回类似的东西

    Microsoft® Windows Server® 2008 标准版

【讨论】:

【解决方案3】:

不幸的是,大多数其他答案都没有提供特定于 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。多年来我一直很困惑,没有统一的方式来获取这些信息。
  • 非常有用的 MS 信息链接。应该注意的是,对于 Win8.1(及以下?),显示的信息是: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 进行比较。
  • 应该是 2020 年公认的答案。嘿,SO Team,这些年来我们可以改变公认的答案吗?这是meta 的问题吗?
  • releaseid 不再适用于 21h1,而是现在使用 displayversion。旧版本不存在
【解决方案4】:
Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer

返回

WindowsProductName    WindowsVersion OsHardwareAbstractionLayer
------------------    -------------- --------------------------
Windows 10 Enterprise 1709           10.0.16299.371 

【讨论】:

  • @not2qubit 真的吗?在我的 Surface Book 2 上大约 1 秒。
  • 获取计算机信息 |选择 WindowsVersion 不再适用于 21h1 - 它显示 2009(与 20h2 相同)
  • 这很烦人......微软是如此......一致。不是。
【解决方案5】:

这将为您提供完整版本的 Windows(包括修订/内部版本号),这与上述所有解决方案不同:

(Get-ItemProperty -Path c:\windows\system32\hal.dll).VersionInfo.FileVersion

结果:

10.0.10240.16392 (th1_st1.150716-1608)

【讨论】:

  • 就我而言,这是最好的解决方案,因为它可以正确报告修订号。其他都不是(至少我已经测试过了)。
  • 这是迄今为止唯一能让我获得完整版本号的解决方案。 然而,并不是system32中的所有文件都会随着每次更新而更新——例如,我的hal.dll仍然显示10.0.10586.0 (th2_release.151029-1700),而winload.exe有10.0.10586.63 (th2_release.160104-1513)。
  • 这里有一个小脚本,可以从 dll/exe 中获取最高构建日期的版本:gist
  • 这依赖于微软方面的实施细节,不能保证他们会继续这样做。它现在可以工作,但如果你希望你的脚本长期工作,你应该避免依赖它。
  • @Jaykul 好吧,我不同意,有两个原因。 (1)因为那些“1803”之类的数字并不总是可用的(例如在Win8上),那么应该在那里使用什么? (2) 没有技术原因为什么应该只有一个正确的version。操作系统是由部分构建(和更新)的,即内核、HAL、UBR 和 功能 等。所以我们应该真正显示所有这些。在这方面,我认为BuildLabEx、Kernel 和HAL(按此顺序)将是提供更合适的版本 的最合适方式。但既然你似乎知道什么是错,你应该发布什么是正确。
【解决方案6】:

从 PowerShell 5 开始:

Get-ComputerInfo
Get-ComputerInfo -Property Windows*

我认为这个命令几乎尝试了迄今为止发现的 1001 种不同的方法来收集系统信息......

【讨论】:

  • 我得到的部分响应很奇怪......我在 Windows 10 1909 上,但“WindowsCurrentVersion”是 6.3。我认为那将是 10,因为 6.3 是 Windows 8.1。否则,我喜欢这个命令提供的信息
【解决方案7】:

如果您想区分 Windows 8.1 (6.3.9600) 和 Windows 8 (6.2.9200),请使用

(Get-CimInstance Win32_OperatingSystem).Version 

获取正确的版本。 [Environment]::OSVersion 在 Windows 8.1 中无法正常工作(它返回 Windows 8 版本)。

【讨论】:

  • 请注意[Environment]::OSVersion 在windows-10 中有效,OSVersion.Version.Major 返回 10。
  • (Get-CimInstance Win32_OperatingSystem).Version 和 [Environment]::OSVersion 都为我工作并返回相同的结果:6.3.9600.0
  • 不幸的是 6.3.9600 不仅仅是 Win 8.1,服务器 2012 R2 也返回相同的内部版本号。
【解决方案8】:

我正在提炼其中一个answers

我在尝试匹配 w​​inver.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 }

【讨论】:

    【解决方案9】:

    如果您试图破译 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

    Microsoft Windows 10 家庭版 64 位 1703

    【讨论】:

      【解决方案10】:

      我采用了上面的脚本并对其进行了一些调整以得出以下结论:

      $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 Home 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)
      【解决方案11】:

      要在 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))"
      

      【讨论】:

      • 它还与 Windows 10 中“设置 > 系统 > 关于”中的版本相匹配。并且获得了正确的更新构建版本,这在我的机器上没有许多解决方案 ?
      • 这停止了对我的工作。 WinVer.exe 显示Version 20H2 (OS Build 19042.804),但这给出了Version 2009 (OS Build 19042.804)。将ReleaseId 更改为DisplayVersion 可以解决此问题,但旧操作系统没有DisplayVersion。
      • releaseid 不再适用于 21h1,而是现在使用 displayversion。旧版本不存在
      【解决方案12】:

      正如 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}
      }
      

      【讨论】:

        【解决方案13】:

        用途:

        Get-WmiObject -class win32_operatingsystem -computer computername | Select-Object Caption
        

        【讨论】:

        • 也可以使用这个获取版本号:Get-WmiObject -class win32_operatingsystem |选择版本
        • 您可以通过显示输出来改进此答案。
        【解决方案14】:

        应该像这样简单:

        Get-ComputerInfo  | select windowsversion
        

        【讨论】:

        • 获取计算机信息 |选择 WindowsVersion 不再适用于 21h1 - 它显示 2009(与 20h2 相同)
        • 我需要添加,使用Powershell执行这个命令,谢谢
        【解决方案15】:

        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 
        

        【讨论】:

          【解决方案16】:

          这确实是一个很长的话题,可能是因为答案虽然正确,但并没有解决基本问题。 我偶然发现了这个网站: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
          

          注意:如您所见,我将上述内容仅用于服务器系统,但它可以轻松应用于工作站,甚至可以巧妙地扩展以支持两者......但我将把它留给你。

          尽情享受,玩得开心!

          【讨论】:

          • 我很乐意将其应用于工作站,但 PS 如何检测它是在 Windows Server 还是简单的 Windows PC 上运行?
          • Windows 客户端操作系统版本具有不同的构建值,请查看此 Microsoft 链接:docs.microsoft.com/en-us/windows/win32/sysinfo/…。另外你需要检查产品类型:(Get-CimInstance Win32_OperatingSystem).productType,如果它等于1那么它是一个客户端
          • The link you gave 没有给出构建值但是""+[Environment]::OSVersion.Version.Major+"."+[Environment]::OSVersion.Version.Minor 你有包含微软官方构建表的链接吗?
          • @SebMa 请参阅Windows 10 release information,在网页底部有每个已发布版本的构建表以及每个版本的构建号。这里是Windows 11 release information。
          【解决方案17】:

          你们太努力了。这适用于使用 Enter-PSSession 的本地或远程会话 - 试一试。

          您只需输入:

          cmd ?
          
          

          Microsoft Windows [版本 10.0.19042.1237]

          【讨论】:

            【解决方案18】:
            (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name BuildLabEx).BuildLabEx
            

            【讨论】:

              【解决方案19】:
              (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Client.OS.rs2.amd64').version
              

              根据 Tim 之前的回答,这个特定位置的好处是该属性已经处于我所说的首选格式。

              【讨论】:

                【解决方案20】:

                相当于 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 在“版本”旁边显示的内容。

                我没想到要读这么多才能写出这段代码,我真的希望我不必为 22 小时(或当时的名字)调整它。


                *:微软确实让它变得比它应该的更复杂

                【讨论】:

                  【解决方案21】:

                  这将为您提供 Windows 的完整和正确(与您在运行 winver.exe 时找到的相同版本号)版本(包括修订/内部版本号),与所有其他解决方案(在 Windows 10 上测试)完全不同:

                  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"
                      }
                  }
                  

                  【讨论】:

                  • 我在使用 'localhost' 运行它并在我的 localhost 上使用实际计算机名(由 'hostname' 返回)时出错 - 可以调整此解决方案以允许它从本地机器没有启用服务等?
                  • [xxxxxx] 连接到远程服务器 xxxxxx 失败并显示以下错误消息:客户端无法连接到请求中指定的目标。验证目标上的服务是否正在运行并且正在接受请求。请查阅在目标(最常见的是 IIS 或 WinRM)上运行的 WS-Management 服务的日志和文档。如果目标是 WinRM 服务,在目标上运行以下命令来分析和配置 WinRM 服务:“winrm quickconfig”。欲了解更多信息,[...]
                  • 为我工作。赞成。如果它包含 Windows 10 版本 ID - 1507、1511、1607 等,那将是一个完美的脚本。
                  【解决方案22】:

                  我搜索了很多以找出确切的版本,因为 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
                  

                  【讨论】:

                    【解决方案23】:

                    使用Windows Powershell,可以通过以下方式获取你需要的数据

                    说明:

                    (Get-WmiObject -class Win32_OperatingSystem).Caption
                    

                    ReleaseId:

                    (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
                    

                    版本:

                    (Get-CimInstance Win32_OperatingSystem).version
                    

                    【讨论】:

                    • releaseid 不再适用于 21h1,而是现在使用 displayversion。旧版本不存在
                    【解决方案24】:

                    [已解决]

                    #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-----------------#
                    

                    【讨论】:

                    • 欢迎来到 SO!当您回答问题时,请尝试解释一下。在这种情况下,还有 20 条回复,因此请考虑公开您的优点。
                    【解决方案25】:

                    您也可以通过检查 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"}
                    

                    【讨论】:

                      【解决方案26】:

                      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 '对于服务器操作系统'

                      【讨论】:

                      • 嗨,蒂姆,您能解释一下这有助于找到问题的答案吗?我碰巧现在这些是 Windows 注册表地址,但不是每个人都会。此外,还询问了如何在 PowerShell 中执行此操作。您可以在 PowerShell 中添加代码来说明如何执行此操作吗?
                      【解决方案27】:

                      我只想完成一个小脚本。我们使用了之前回答过的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 或更早版本.. 停止移动到新的操作系统。

                      【讨论】:

                        【解决方案28】:

                        powershell 中 C:\ 提示符或 cmd 提示符窗口中的 systeminfo 提供操作系统名称版本配置制造商等等...

                        【讨论】:

                        • 这在其他答案中已经提到了。
                        • 这是否适用于 MS Windows 10?
                        【解决方案29】:

                        除了其他答案之外,这里还有一些可以使用 PowerShell 检索的有用信息:

                        通过 PowerShell 查询操作系统和硬件信息:

                        查询通用操作系统(操作系统)信息:

                        查看操作系统名称的最快方法:

                        cmd ?
                        

                        #Using Get-ComputerInfo:

                        Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer
                        

                        #Using 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 &lt;your_ip_address&gt;)您将在第一行看到您的“主机名”。

                        查询当前(登录)用户:

                        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
                        

                        OR(以 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"
                        

                        #查询内存,包括频率/速度:

                        Get-CimInstance win32_physicalmemory | Format-Table Manufacturer,Banklabel,Configuredclockspeed,Devicelocator,Capacity,Serialnumber –autosize
                        

                        如前所述,这个答案有点超出了所提出的问题,但对于那些想要使用 PowerShell 获得更多操作系统或硬件信息的人来说可能很有用。

                        【讨论】:

                          【解决方案30】:
                          $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
                             }
                          

                          【讨论】:

                            猜你喜欢
                            • 2013-03-09
                            • 1970-01-01
                            • 1970-01-01
                            • 2018-03-14
                            • 2023-02-08
                            • 2018-10-12
                            • 1970-01-01
                            • 1970-01-01
                            • 2010-12-09
                            相关资源
                            最近更新 更多