如何使用 PowerShell 检查 Windows 11 更新状态

Jacki

保持 Windows 11 保持最新对于安全性和性能至关重要。虽然您可以手动检查 Windows 更新历史记录,但使用 PowerShell 脚本可以提供更快、更详细的方式来查看系统的更新状态。本文将向您展示如何使用 PowerShell 脚本检查当前的 Windows 11 补丁信息和可用更新。

我们将使用的 PowerShell 脚本提供有关 Windows 11 安装的全面信息,包括:

  • 当前操作系统版本
  • 操作系统编辑
  • 操作系统版本号
  • 最新安装的更新(包含知识库编号和信息链接)
  • 最新可用更新

该脚本适用于 Windows 11 和 Windows 10 系统。让我们来看看如何使用它。

步骤一:以管理员身份打开 PowerShell。右键单击“开始”按钮,然后从菜单中选择“Windows PowerShell(管理员)”。

步骤2:复制以下 PowerShell 脚本并将其另存为桌面上的 .ps1 文件(例如“Get-WindowsUpdateInfo.ps1”):

[CmdletBinding()]
Param(
[switch]$ListAllAvailable,
[switch]$ExcludePreview,
[switch]$ExcludeOutofBand
)
$ProgressPreference="SilentlyContinue"
$URI = "https://aka.ms/WindowsUpdateHistory"

Function Get-MyWindowsVersion {
[CmdletBinding()]
Param
(
$ComputerName = $env:COMPUTERNAME
)

$Table = New-Object System.Data.DataTable
$Table.Columns.AddRange(@("ComputerName","Windows Edition","Version","OS Build"))
$ProductName = (Get-ItemProperty 'HKLM:SOFTWAREMicrosoftWindows NTCurrentVersion' -Name ProductName).ProductName
Try
{
$Version = (Get-ItemProperty 'HKLM:SOFTWAREMicrosoftWindows NTCurrentVersion' -Name ReleaseID -ErrorAction Stop).ReleaseID
}
Catch
{
$Version = "N/A"
}
$CurrentBuild = (Get-ItemProperty 'HKLM:SOFTWAREMicrosoftWindows NTCurrentVersion' -Name CurrentBuild).CurrentBuild
$UBR = (Get-ItemProperty 'HKLM:SOFTWAREMicrosoftWindows NTCurrentVersion' -Name UBR).UBR
$OSVersion = $CurrentBuild + "." + $UBR
$TempTable = New-Object System.Data.DataTable
$TempTable.Columns.AddRange(@("ComputerName","Windows Edition","Version","OS Build"))
[void]$TempTable.Rows.Add($env:COMPUTERNAME,$ProductName,$Version,$OSVersion)

Return $TempTable
}

Function Convert-ParsedArray {
Param($Array)

$ArrayList = New-Object System.Collections.ArrayList
foreach ($item in $Array)
{ 
[void]$ArrayList.Add([PSCustomObject]@{
Update = $item.outerHTML.Split('>')[1].Replace('</a','').Replace('—',' - ')
KB = "KB" + $item.href.Split('/')[-1]
InfoURL = "https://support.microsoft.com" + $item.href
OSBuild = $item.outerHTML.Split('(OS ')[1].Split()[1]
})
}
Return $ArrayList
}

If ($PSVersionTable.PSVersion.Major -ge 6)
{
$Response = Invoke-WebRequest -Uri $URI -ErrorAction Stop
}
else 
{
$Response = Invoke-WebRequest -Uri $URI -UseBasicParsing -ErrorAction Stop
}

If (!($Response.Links))
{ throw "Response was not parsed as HTML"}
$VersionDataRaw = $Response.Links | where {$_.outerHTML -match "supLeftNavLink" -and $_.outerHTML -match "KB"}
$CurrentWindowsVersion = Get-MyWindowsVersion -ErrorAction Stop

If ($ListAllAvailable)
{
If ($ExcludePreview -and $ExcludeOutofBand)
{
$AllAvailable = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.') -and $_.outerHTML -notmatch "Preview" -and $_.outerHTML -notmatch "Out-of-band"}
}
ElseIf ($ExcludePreview)
{
$AllAvailable = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.') -and $_.outerHTML -notmatch "Preview"}
}
ElseIf ($ExcludeOutofBand)
{
$AllAvailable = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.') -and $_.outerHTML -notmatch "Out-of-band"}
}
Else
{
$AllAvailable = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.')}
}
$UniqueList = (Convert-ParsedArray -Array $AllAvailable) | Sort OSBuild -Descending -Unique
$Table = New-Object System.Data.DataTable
[void]$Table.Columns.AddRange(@('Update','KB','InfoURL'))
foreach ($Update in $UniqueList)
{
[void]$Table.Rows.Add(
$Update.Update,
$Update.KB,
$Update.InfoURL
)
}
Return $Table
}

$CurrentPatch = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'} | Select -First 1
If ($ExcludePreview -and $ExcludeOutofBand)
{
$LatestAvailablePatch = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.') -and $_.outerHTML -notmatch "Out-of-band" -and $_.outerHTML -notmatch "Preview"} | Select -First 1
}
ElseIf ($ExcludePreview)
{
$LatestAvailablePatch = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.') -and $_.outerHTML -notmatch "Preview"} | Select -First 1
}
ElseIf ($ExcludeOutofBand)
{
$LatestAvailablePatch = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.') -and $_.outerHTML -notmatch "Out-of-band"} | Select -First 1
}
Else
{
$LatestAvailablePatch = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.')} | Select -First 1
}

$Table = New-Object System.Data.DataTable
[void]$Table.Columns.AddRange(@('OSVersion','OSEdition','OSBuild','CurrentInstalledUpdate','CurrentInstalledUpdateKB','CurrentInstalledUpdateInfoURL','LatestAvailableUpdate','LastestAvailableUpdateKB','LastestAvailableUpdateInfoURL'))
[void]$Table.Rows.Add(
$CurrentWindowsVersion.Version,
$CurrentWindowsVersion.'Windows Edition',
$CurrentWindowsVersion.'OS Build',
$CurrentPatch.outerHTML.Split('>')[1].Replace('</a','').Replace('—',' - '),
"KB" + $CurrentPatch.href.Split('/')[-1],
"https://support.microsoft.com" + $CurrentPatch.href,
$LatestAvailablePatch.outerHTML.Split('>')[1].Replace('</a','').Replace('—',' - '),
"KB" + $LatestAvailablePatch.href.Split('/')[-1],
"https://support.microsoft.com" + $LatestAvailablePatch.href
)
Return $Table

步骤3:通过在 PowerShell 中输入以下命令来运行脚本:

.Get-WindowsUpdateInfo.ps1

此命令将显示您当前的 Windows 版本、内部版本号和最新安装的更新。

第4步:要从结果中排除预览和带外更新,请使用以下命令:

参见:在 Microsoft Entra 和 PowerShell 中获取 MFA 状态

.Get-WindowsUpdateInfo.ps1 -ExcludePreview -ExcludeOutofBand

如果您只想关注标准累积更新,则此选项非常有用。

第5步:要查看您的 Windows 版本的所有可用更新,请使用以下命令:

.Get-WindowsUpdateInfo.ps1 -ListAllAvailable

这将向您显示 Microsoft 为您的特定 Windows 版本发布的所有更新的列表。

第6步:如果您想查看除预览和带外更新之外的所有可用更新,请使用以下命令:

.Get-WindowsUpdateInfo.ps1 -ListAllAvailable -ExcludePreview -ExcludeOutofBand

此命令结合了选项,为您提供标准更新的重点列表。

使用此 PowerShell 脚本可以简化检查 Windows 11 更新状态的过程。对于管理多个系统的 IT 专业人员或想要获得有关 Windows 更新的详细信息而无需浏览多个菜单的用户来说,它特别有用。