Bạn có thể dùng module PowerShell PSWindowsUpdate để quản lý Windows Update từ dòng lệnh. Module PSWindowsUpdate không tích hợp sẵn trong Windows và có sẵn để cài từ kho PowerShell Gallery. PSWindowsUpdate cho phép quản trị viên kiểm tra, cài đặt, gỡ bỏ, ẩn update từ xa trên server và workstation Windows. Module PSWindowsUpdate đặc biệt giá trị để quản lý update trên Windows Server Core hoặc Hyper-V Server (không có GUI), và khi cấu hình Windows image ở chế độ audit.
Cài đặt module PSWindowsUpdate
Bạn có thể cài module PSWindowsUpdate trên Windows 10/11 và Windows Server 2022/2019/2016 từ kho online (PSGallery) dùng PackageManagement với một lệnh:
Install-Module -Name PSWindowsUpdate -Force
Sau khi cài xong, kiểm tra gói:
Get-Package -Name PSWindowsUpdate
Khi cài module PowerShell trên Windows phiên bản cũ 2012R2/Windows 8.1 và dưới, bạn có thể nhận lỗi:
Install-Module: Unable to download from URI.Unable to download the list of available providers. Check your internet connection.
Để cài module, bạn cần dùng giao thức TLS 1.2 cho kết nối. Bật nó cho phiên PowerShell hiện tại bằng lệnh:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Nếu bạn có phiên bản Windows cũ (Windows 7/8.1/Windows Server 2008 R2/2012 R2) hoặc không có Internet trực tiếp, bạn có thể cài PSWindowsUpdate thủ công (xem hướng dẫn “How to install PowerShell modules offline?”).
- Tải module PSWindowsUpdate về máy tính online bất kỳ:
Save-Module –Name PSWindowsUpdate –Path C:\ps\; - Copy module vào thư mục sau trên máy tính đích
%WINDIR%\System32\WindowsPowerShell\v1.0\Modules;
- Cấu hình policy thực thi script PowerShell:
Set-ExecutionPolicy –ExecutionPolicy RemoteSigned -force - Bây giờ bạn có thể import module vào phiên PowerShell:
Import-Module PSWindowsUpdate
Lưu ý: Trong Windows 7/Windows Server 2008 R2, khi import module PSWindowsUpdate, lỗi sau có thể xuất hiện: The term “Unblock-File” is not recognized as the name of a cmdlet. Nguyên nhân là module dùng một số hàm chỉ xuất hiện từ PowerShell 3.0. Để dùng các hàm này, bạn phải cập nhật phiên bản PowerShell hoặc xóa dòng | Unblock-File từ file PSWindowsUpdate.psm1 thủ công.
Sau khi cài module PSWindowsUpdate trên máy tính, bạn có thể cài từ xa nó trên máy tính hoặc server khác dùng cmdlet Update-WUModule. Ví dụ, để copy module PSWindowsUpdate từ máy tính của bạn đến hai host từ xa, chạy lệnh (cần truy cập server từ xa qua giao thức WinRM):
$Targets = "lon-fs02", "lon-db01"
Update-WUModule -ComputerName $Targets –Local
Để lưu (xuất) module PoSh vào thư mục chia sẻ mạng để import sau trên máy tính khác, chạy:
Save-Module -Name PSWindowsUpdate –Path \\lon-fs02\psmodules\
Danh sách cmdlet PSWindowsUpdate
Bạn có thể hiển thị danh sách cmdlet có sẵn trong module PSWindowsUpdate như sau:
get-command -module PSWindowsUpdate
Mô tả ngắn gọn cách dùng các lệnh module:
- Clear-WUJob – dùng Get-WUJob xóa WUJob trong Task Scheduler;
- Download-WindowsUpdate (alias cho
Get-WindowsUpdate –Download) — lấy danh sách update và tải xuống; - Get-WUInstall, Install-WindowsUpdate (alias cho
Get-WindowsUpdate –Install) – cài đặt Windows update; - Hide-WindowsUpdate (alias cho
Get-WindowsUpdate -Hide:$false) – ẩn update; - Uninstall-WindowsUpdate – gỡ update dùng Remove-WindowsUpdate;
- Add-WUServiceManager – đăng ký update server (Windows Update Service Manager) trên máy tính;
- Enable-WURemoting — bật rule Windows Defender firewall cho phép dùng từ xa cmdlet PSWindowsUpdate;
- Get-WindowsUpdate (Get-WUList) — hiển thị danh sách update khớp tiêu chí, cho phép tìm và cài update. Đây là cmdlet chính của module PSWindowsUpdate. Cho phép tải và cài update từ server WSUS hoặc Microsoft Update. Cho chọn category update, update cụ thể và quy tắc restart máy khi cài update;
- Get-WUApiVersion – lấy phiên bản Windows Update Agent trên máy tính;
- Get-WUHistory – hiển thị danh sách update đã cài (lịch sử update);
- Get-WUInstallerStatus — kiểm tra trạng thái dịch vụ Windows Installer;
- Get-WUJob – kiểm tra task cập nhật WUJob trong Task Scheduler;
- Get-WULastResults — ngày tìm kiếm và cài update cuối (LastSearchSuccessDate và LastInstallationSuccessDate);
- Get-WURebootStatus — kiểm tra xem có cần reboot áp dụng update cụ thể không;
- Get-WUServiceManager – liệt kê nguồn update;
- Get-WUSettings – lấy cài đặt client Windows Update;
- Invoke-WUJob – gọi từ xa WUJobs task trong Task Scheduler thực thi lệnh PSWindowsUpdate ngay;
- Remove-WindowsUpdate – cho phép gỡ update bằng KB ID;
- Remove-WUServiceManager – tắt Windows Update Service Manager;
- Set-PSWUSettings – lưu cài đặt module PSWindowsUpdate vào file XML;
- Set-WUSettings – cấu hình cài đặt client Windows Update;
- Update-WUModule – cập nhật module PSWindowsUpdate (có thể cập nhật module trên máy tính từ xa bằng copy từ máy hiện tại, hoặc cập nhật từ PSGallery);
- Reset-WUComponents – cho phép reset Windows Update agent trên máy tính về trạng thái mặc định.
Để kiểm tra cài đặt client Windows Update hiện tại, chạy lệnh:
Get-WUSettings
ComputerName : WKS5S2N39S2
WUServer : https://MN-WSUS:8530
WUStatusServer : https://MN-WSUS:8530
AcceptTrustedPublisherCerts : 1
ElevateNonAdmins : 1
DoNotConnectToWindowsUpdateInternetLocations : 1
TargetGroupEnabled : 1
TargetGroup : ServersProd
NoAutoUpdate : 0
AUOptions : 3 - Notify before installation
ScheduledInstallDay : 0 - Every Day
ScheduledInstallTime : 3
UseWUServer : 1
AutoInstallMinorUpdates : 0
AlwaysAutoRebootAtScheduledTime : 0
DetectionFrequencyEnabled : 1
DetectionFrequency : 4
Trong ví dụ này, Windows Update agent trên máy tính được cấu hình bằng GPO nhận update từ server WSUS local.
Cmdlet Reset-WUComponents -Verbose cho phép reset tất cả cài đặt Windows Update Agent, đăng ký lại thư viện, và khôi phục dịch vụ wususerv về mặc định.
Quét và tải Windows Update với PowerShell
Bạn có thể liệt kê update có sẵn cho máy tính hiện tại trên server update dùng lệnh Get-WindowsUpdate hoặc Get-WUList.
Để kiểm tra danh sách update có sẵn trên máy tính từ xa, chạy lệnh:
Get-WUList –ComputerName server2
Bạn có thể kiểm tra Windows của bạn nhận update từ đâu. Chạy lệnh:
Get-WUServiceManager
ServiceID IsManaged IsDefault Name
--------- --------- --------- ----
8b24b027-1dee-babb-9a95-3517dfb9c552 False False DCat Flighting Prod
855e8a7c-ecb4-4ca3-b045-1dfa50104289 False False Windows Store (DCat Prod)
3da21691-e39d-4da6-8a4b-b43877bcb1b7 True True Windows Server Update Service
9482f4b4-e343-43b6-b170-9a65bc822c77 False False Windows Update
Như bạn thấy, máy tính được cấu hình nhận update từ server WSUS local (Windows Server Update Service = True). Trong trường hợp này, bạn nên thấy danh sách update được phê duyệt cho máy tính.
Nếu muốn quét máy tính đối chiếu server Microsoft Update trên Internet (ngoài Windows update, server này chứa update Office và sản phẩm Microsoft khác), chạy lệnh:
Get-WUlist -MicrosoftUpdate
Bạn sẽ nhận cảnh báo:
Get-WUlist : Service Windows Update was not found on computer. Use Get-WUServiceManager to get registered service.
Để cho phép quét trên Microsoft Update, chạy lệnh:
Add-WUServiceManager -ServiceID "7971f918-a847-4430-9279-4a52d1efe18d" -AddServiceFlag 7
Bây giờ bạn có thể quét đối chiếu Microsoft Update. Trong trường hợp này, tìm thêm update cho Microsoft Visual C++ 2008 và Microsoft Silverlight.
Để kiểm tra phiên bản Windows Update Agent trên máy tính, chạy lệnh:
Get-WUApiVersion
ComputerName PSWindowsUpdate PSWUModuleDll ApiVersion WuapiDllVersion
------------ --------------- ------------- ---------- ---------------
DESKTOP-J... 2.1.1.2 2.2.0.2 8.0 10.0.19041.1320
Để loại bỏ sản phẩm hoặc KB cụ thể khỏi danh sách update máy tính nhận, bạn có thể loại trừ chúng theo:
- Category (
-NotCategory); - Title (
-NotCategory); - Số update (
-NotKBArticleID).
Ví dụ, loại trừ OneDrive, driver update, và KB cụ thể khỏi danh sách:
Get-WUlist -NotCategory "Drivers" -NotTitle "OneDrive" -NotKBArticleID KB4489873
Cài đặt Windows Update với PowerShell (Install-WindowsUpdate)
Để tự động tải và cài tất cả update có sẵn cho thiết bị Windows từ server Windows Update (thay vì WSUS local), chạy lệnh:
Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot
Tham số AcceptAll chấp nhận cài tất cả gói update, và AutoReboot cho phép Windows tự restart sau khi cài update.
Bạn cũng có thể dùng các tùy chọn:
- IgnoreReboot – tắt tự động reboot;
- ScheduleReboot – đặt thời điểm chính xác restart máy tính.
Bạn có thể lưu lịch sử cài update vào file log (có thể dùng thay file WindowsUpdate.log).
Install-WindowsUpdate -AcceptAll -Install -AutoReboot | Out-File "c:\logs\$(get-date -f yyyy-MM-dd)-WindowsUpdate.log" -force
Bạn có thể cài chỉ gói update cụ thể bằng số KB:
Get-WindowsUpdate -KBArticleID KB2267602, KB4533002 -Install
Trong trường hợp này, bạn cần xác nhận cài từng update thủ công.
Nếu muốn loại trừ update nhất định khỏi danh sách cài, chạy lệnh:
Install-WindowsUpdate -NotCategory "Drivers" -NotTitle OneDrive -NotKBArticleID KB4011670 -AcceptAll -IgnoreReboot
Cài Windows Update trên máy tính từ xa với PowerShell
Module PSWindowsUpdate cho phép cài update từ xa trên nhiều workstation hoặc server cùng lúc (module PSWindowsUpdate phải cài/import trên các máy này). Rất tiện vì admin không phải đăng nhập thủ công lên host Windows từ xa cài update. WinRM phải bật và cấu hình trên máy tính từ xa (thủ công hoặc qua GPO).
Gần như tất cả cmdlet module PSWindowsUpdate cho phép quản lý và cài Windows update trên máy tính từ xa với thuộc tính –Computername.
Cài module PSWindowsUpdate trên máy tính từ xa và cho phép truy cập qua cổng RPC động đến tiến trình dllhost.exe trong Windows Defender Firewall. Bạn có thể dùng cmdlet Invoke-Command cấu hình module PSWindowsUpdate trên máy tính từ xa:
$Targets = "lon-fs02", "lon-db01"
Invoke-Command -ComputerName $Target -ScriptBlock {Set-ExecutionPolicy RemoteSigned -force }
Invoke-Command -ComputerName $Target -ScriptBlock {Import-Module PSWindowsUpdate; Enable-WURemoting}
Module PSWindowsUpdate có thể dùng quản lý từ xa Windows update cả trên máy tính trong AD domain và workgroup (yêu cầu cấu hình PowerShell Remoting cho môi trường workgroup).
Để quản lý update trên máy tính từ xa, bạn cần thêm hostname vào danh sách trusted host winrm hoặc cấu hình PowerShell Remoting (WinRM) qua HTTPS:
winrm set winrm/config/client '@{TrustedHosts="server1,server2,…"}'
Hoặc với PowerShell :
Set-Item wsman:\localhost\client\TrustedHosts -Value server1 -Force
Lệnh sau sẽ cài tất cả update có sẵn trên ba host Windows từ xa:
$ServerNames = "server1, server2, server3"
Invoke-WUJob -ComputerName $ServerNames -Script {ipmo PSWindowsUpdate; Install-WindowsUpdate -AcceptAll | Out-File C:\Windows\PSWindowsUpdate.log } -RunNow -Confirm:$false -Verbose -ErrorAction Ignore
Cmdlet Invoke-WUJob (trước gọi Invoke-WUInstall) sẽ tạo task scheduler trên máy tính từ xa chạy dưới tài khoản SYSTEM local.
Bạn có thể chỉ định thời điểm chính xác cài Windows update:
Invoke-WUJob -ComputerName $ServerNames -Script {ipmo PSWindowsUpdate; Install-WindowsUpdate –AcceptAll -AutoReboot | Out-File C:\Windows\PSWindowsUpdate.log } -Confirm:$false -TriggerDate (Get-Date -Hour 22 -Minute 0 -Second 0)
Bạn có thể kiểm tra trạng thái task cài update dùng Get-WUJob:
Get-WUJob -ComputerName $ServerNames
Nếu lệnh trả về danh sách rỗng, nghĩa là task cài update trên tất cả máy tính đã hoàn thành.
Bạn có thể cài update trên máy tính từ xa và gửi báo cáo email cho admin:
Install-WindowsUpdate -ComputerName nysrv1 -MicrosoftUpdate -AcceptAll - IgnoreReboot -SendReport –PSWUSettings @{SmtpServer="smtp.woshub.com";From="update_alert@woshub.com";To="wsus_admin@woshub.com";Port=25} -Verbose
Kiểm tra lịch sử Windows Update với PowerShell (Get-WUHistory)
Dùng cmdlet Get-WUHistory, bạn có thể lấy danh sách update đã cài trên máy tính trước đó tự động hoặc thủ công.
Bạn có thể lấy thông tin ngày cài update cụ thể:
Get-WUHistory| Where-Object {$_.Title -match "KB4517389"} | Select-Object *|ft
Để biết update cụ thể đã cài trên nhiều máy tính từ xa, dùng code PowerShell:
"server1","server2" | Get-WUHistory| Where-Object {$_.Title -match "KB4011634"} | Select-Object *|ft
Kiểm tra xem máy tính có cần restart sau cài update (pending reboot):
Get-WURebootStatus –ComputerName WKS21TJS
Kiểm tra giá trị thuộc tính RebootRequired và RebootScheduled.
Bạn có thể tạo báo cáo ngày cài update cuối trên tất cả máy tính trong domain dùng cmdlet Get-ADComputer (từ module Active Directory for PowerShell):
$Computers=Get-ADComputer -Filter {enabled -eq "true" -and OperatingSystem -Like '*Windows*' }
Foreach ($Computer in $Computers)
{
Get-WULastResults -ComputerName $Computer.Name|select ComputerName, LastSearchSuccessDate, LastInstallationSuccessDate
}
Tương tự, bạn có thể tìm máy tính chưa cài update quá 60 ngày và hiển thị kết quả trong bảng Out-GridView tương tác:
$result=@()
Foreach ($Computer in $Computers) {
$result+= Get-WULastResults -ComputerName $Computer.Name
}
$result| Where-Object { $_.LastInstallationSuccessDate -lt ((Get-Date).AddDays(-60)) }| Out-GridView
Gỡ bỏ Windows Update với PowerShell (Remove-WindowsUpdate)
Bạn có thể dùng cmdlet Remove-WindowsUpdate để gỡ bỏ đúng cách update với PowerShell. Chỉ định số KB làm đối số tham số KBArticleID. Để trì hoãn restart tự động, thêm tùy chọn –NoRestart:
Remove-WindowsUpdate -KBArticleID KB4489873 -NoRestart
Cách ẩn Windows Update với PowerShell?
Bạn có thể ẩn update cụ thể để chúng không bao giờ được cài bởi dịch vụ Windows Update trên máy tính (thường ẩn driver update). Ví dụ, ẩn update KB4489873 và KB4489243, chạy lệnh:
$HideList = "KB4489873", "KB4489243"
Get-WindowsUpdate -KBArticleID $HideList –Hide
Bây giờ lần sau quét update dùng Get-WUlist, update ẩn không hiển thị trong danh sách update có sẵn cài.
Cách hiển thị danh sách update ẩn trên máy tính này:
Get-WindowsUpdate –IsHidden
Chú ý thuộc tính H (Hidden) xuất hiện trong cột Status của update ẩn.
Để hiện lại một số update, chạy lệnh:
Get-WindowsUpdate -KBArticleID $HideList -WithHidden -Hide:$false
hoặc:
Show-WindowsUpdate -KBArticleID $HideList
Cho những ai không thoải mái trong console PowerShell, tôi khuyên công cụ đồ họa Windows Update MiniTool quản lý update trong Windows 10/11 và Windows Server 2022/2019.