Je kunt met PowerShell eenvoudig Windows updates voor Windows installeren. Hiervoor hoef je echt niet over Windows Server en/of WSUS te beschikken, Windows 10 met PowerShell 5.1 is voldoende om NuGet en PSWindowsUpdate module te kunnen gebruiken. Windows updates installeer je dan via PowerShell eenvoudig vanaf Windows Update. Ga als volgt te werk:

PSWindowsUpdate PowerShell module installeren

Looking for my English post Install Windows Updates using PowerShell? It’s there on Sysadmins of the North :-)

Om de PSWindowsUpdate PowerShell module beschikbaar te maken moet je een PowerShell prompt als administrator starten. Via Start, zoek naar PowerShell en kies de optie Run as Administrator.

Of gebruik de shortcut Windowstoets-X en kies Windows PowerShell (Admin).

Indien nodig moet je eerst het uitvoerbeleid van PowerShell instellen op RemoteSigned: Set-ExecutionPolicy RemoteSigned. Voer hierna de volgende commando’s uit:

  • Install-PackageProvider -Name NuGet -Force (werkt alleen met PowerShell 5.1, niet 7)
  • Install-Module -Name PSWindowsUpdate -Force

De module PSWindowsUpdate is nu op je systeem geïnstalleerd. Met Get-Command vraag je op welke Cmdlets en aliassen er zijn. Geef de -Module parameter mee:

PSWindowsUpdate beschikbare Cmdlets
Get-Command -Module PSWindowsUpdate

CommandType Name Version Source
----------- ---- ------- ------
Alias Clear-WUJob 2.1.1.2 PSWindowsUpdate
Alias Download-WindowsUpdate 2.1.1.2 PSWindowsUpdate
Alias Get-WUInstall 2.1.1.2 PSWindowsUpdate
Alias Get-WUList 2.1.1.2 PSWindowsUpdate
Alias Hide-WindowsUpdate 2.1.1.2 PSWindowsUpdate
Alias Install-WindowsUpdate 2.1.1.2 PSWindowsUpdate
Alias Show-WindowsUpdate 2.1.1.2 PSWindowsUpdate
Alias UnHide-WindowsUpdate 2.1.1.2 PSWindowsUpdate
Alias Uninstall-WindowsUpdate 2.1.1.2 PSWindowsUpdate
Cmdlet Add-WUServiceManager 2.1.1.2 PSWindowsUpdate
Cmdlet Enable-WURemoting 2.1.1.2 PSWindowsUpdate
Cmdlet Get-WindowsUpdate 2.1.1.2 PSWindowsUpdate
Cmdlet Get-WUApiVersion 2.1.1.2 PSWindowsUpdate
Cmdlet Get-WUHistory 2.1.1.2 PSWindowsUpdate
Cmdlet Get-WUInstallerStatus 2.1.1.2 PSWindowsUpdate
Cmdlet Get-WUJob 2.1.1.2 PSWindowsUpdate
Cmdlet Get-WULastResults 2.1.1.2 PSWindowsUpdate
Cmdlet Get-WURebootStatus 2.1.1.2 PSWindowsUpdate
Cmdlet Get-WUServiceManager 2.1.1.2 PSWindowsUpdate
Cmdlet Get-WUSettings 2.1.1.2 PSWindowsUpdate
Cmdlet Invoke-WUJob 2.1.1.2 PSWindowsUpdate
Cmdlet Remove-WindowsUpdate 2.1.1.2 PSWindowsUpdate
Cmdlet Remove-WUServiceManager 2.1.1.2 PSWindowsUpdate
Cmdlet Set-PSWUSettings 2.1.1.2 PSWindowsUpdate
Cmdlet Set-WUSettings 2.1.1.2 PSWindowsUpdate
Cmdlet Update-WUModule 2.1.1.2 PSWindowsUpdate

Zoeken naar beschikbare Windows Updates

Zoeken naar Windows Updates doe je nu eenvoudig met Get-WindowsUpdate of Get-WUInstall:

Get-WindowsUpdate

ComputerName Status KB Size Title
------------ ------ -- ---- -----
DESKTOP-J... ------- KB2267602 574MB Security Intelligence Update for Windows Defender Antivirus - KB2267602 (Ve...

Scan naar (detecteer) nieuwe updates met Microsoft.Update.AutoUpdate COM-object in PowerShell: (new-object -Comobject Microsoft.Update.AutoUpdate).detectnow()

Gevonden updates installeren

Zijn er updates beschikbaar? Dan installeer je deze update eenvoudig met Install-WindowsUpdate, wat een alias is van Get-WindowsUpdate -Install:

Install-WindowsUpdate -KBArticleID KB2267602 -Force

Een bevestiging wordt gegeven dat de update is geaccepteerd, gedownload en geïnstalleerd:

X ComputerName Result     KB          Size Title
- ------------ ------ -- ---- -----
1 DESKTOP-J... Accepted KB2267602 574MB Security Intelligence Update for Windows Defender Antivirus - KB2267602 (...
2 DESKTOP-J... Downloaded KB2267602 574MB Security Intelligence Update for Windows Defender Antivirus - KB2267602 (...
3 DESKTOP-J... Installed KB2267602 574MB Security Intelligence Update for Windows Defender Antivirus - KB2267602 (...

Je kunt nog véél meer met deze Windows Update module voor PowerShell, zoals het inplannen (schedulen) van updates, updates installeren op andere computers, enz. De Cmdlet Get-Help Install-WindowsUpdate geeft je al deze informatie.

Zo kun je ook in één keer zoeken naar updates en opgeven alle gevonden updates geaccepteerd en geïnstalleerd moeten worden. Dit doe je met -AcceptAll -Download -Install en eventueel -MicrosoftUpdate om aan te geven dat je Microsoft Windows Update wilt raadplegen in plaats van bijvoorbeeld een lokale WSUS:

Get-WindowsUpdate -AcceptAll -Download -Install -MicrosoftUpdate

X ComputerName Result KB Size Title
- ------------ ------ -- ---- -----
1 DESKTOP-J... Accepted KB4481252 13MB Microsoft Silverlight (KB4481252)
1 DESKTOP-J... Accepted KB3152281 48MB Click-to-Run Update Support
2 DESKTOP-J... Downloaded KB4481252 13MB Microsoft Silverlight (KB4481252)
2 DESKTOP-J... Downloaded KB3152281 48MB Click-to-Run Update Support
3 DESKTOP-J... Installed KB4481252 13MB Microsoft Silverlight (KB4481252)
3 DESKTOP-J... Installed KB3152281 48MB Click-to-Run Update Support

Start het Windows Update GUI venster vanuit cmd.exe (of PowerShell) met de volgende opdracht: %windir%\explorer.exe ms-settings:windowsupdate-action

“Check for updates” GUI activeren vanuit de opdrachtprompt

Ter leering ende vermaeck: je kunt de Check for updates GUI activeren vanuit de opdrachtprompt. Hiervoor gebruik je het UsoClient.exe commando. UsoClient.exe is de Windows 10 equivalent van wuauclt.exe (Windows Update Automatic Update Client) in Windows 7. In deze staat USO voor Update Session Orchestrator.

Het UsoClient.exe commando kent een aantal parameters die je mee kunt geven om het gedrag te sturen. Zo gebruik je de parameter ScanInstallWait om gecombineerd te zoeken naar updates en die direct te installeren:

Start-Process -NoNewWindow "c:\windows\system32\UsoClient.exe" -argument "ScanInstallWait" -Wait

Je kunt ook &UsoClient.exe ScanInstallWait intypen in je PowerShell opdrachtprompt. Merk op dat je geen / hoeft te gebruiken voor de parameter.

Als je dit doet als je de Check for updates GUI erbij geopend hebt, dan zie je de GUI animatie dat er wordt gezocht naar Windows Updates (“Checking for updates…”). Worden die gevonden, dan worden ze ook geïnstalleerd.

De beschikbare parameters voor UsoClient.exe zijn (voor zover bekend):

  • StartScan – start een scan naar beschikbare updates voor Windows
  • StartDownload – download updates
  • StartInstall – installeer gedownloade updates. Dit moet je gebruiken na ScanInstallWait.
  • RefreshSettings – vernieuw de instellingen als er wijzigingen zijn geweest
  • StartInteractiveScan – met deze paramter kan de gebruiker om input of goedkeuring worden gevraagd en/of worden er voortgangsdialogen getoond
  • RestartDevice – herstart de computer om de installatie af te ronden
  • ScanInstallWait – een gecombineerde ‘scan’ en ‘download’ updates. Dit is wat je veelal wilt, in combinatie met “StartInstall” erna.
  • ResumeUpdate – hervat update-installatie na reboot

Zoals gezegd hoef je geen / of – voor de switch te gebruiken. Commando’s voer je simpel in als USOClient.exe ScanInstallWait of USOClient.exe StartInstall.

UsoClient parameters verschillen per Windows versie

Als je wilt weten welke parameters UsoClient ondersteunt op jouw computer, voer dan de volgende PowerShell-opdracht uit. In een beheerders venster:

Get-ScheduledTask -TaskPath '\Microsoft\Windows\UpdateOrchestrator\' | Select-Object @{Expression={$_.TaskName};Label="TaskName"}, @{Expression={$_.Actions.Execute + ' ' + $_.Actions.Arguments};Label="CommandLine"}

De uitvoer is bijvoorbeeld:

TaskName                     CommandLine
-------- -----------
AC Power Install %systemroot%\system32\usoclient.exe StartInstall
Backup Scan %systemroot%\system32\usoclient.exe StartScan
Maintenance Install %systemroot%\system32\usoclient.exe StartInstall
MusUx_LogonUpdateResults %systemroot%\system32\MusNotification.exe LogonUpdateResults
Reboot_AC %systemroot%\system32\MusNotification.exe /RunOnAC RebootDialog
Reboot_Battery %systemroot%\system32\MusNotification.exe /RunOnBattery RebootDialog
Report policies %systemroot%\system32\usoclient.exe ReportPolicies
Schedule Maintenance Work %systemroot%\system32\usoclient.exe StartMaintenanceWork
Schedule Scan %systemroot%\system32\usoclient.exe StartScan
Schedule Scan Static Task %systemroot%\system32\usoclient.exe StartScan
Schedule Wake To Work %systemroot%\system32\usoclient.exe StartWork
Schedule Work %systemroot%\system32\usoclient.exe StartWork
Universal Orchestrator Start %systemroot%\system32\usoclient.exe StartUWork
UpdateModelTask %systemroot%\system32\usoclient.exe StartModelUpdates
USO_UxBroker %systemroot%\system32\MusNotification.exe

Of

TaskName                  CommandLine
-------- -----------
AC Power Download %systemroot%\system32\usoclient.exe StartDownload
Backup Scan %systemroot%\system32\usoclient.exe StartScan
MusUx_UpdateInterval %systemroot%\system32\MusNotification.exe Display
Schedule Scan %systemroot%\system32\usoclient.exe StartScan
Schedule Scan Static Task %systemroot%\system32\usoclient.exe StartScan
USO_UxBroker %systemroot%\system32\MusNotification.exe

E-mail versturen met PowerShell, bijvoorbeeld als het installeren van updates klaar is of mislukt. Handig!

Conclusie PSWindowsUpdate

De conclusie van werken met PSWindowsUpdate is dat je eenvoudig Windows updates installeert met PowerShell en deze PSWindowsUpdate module. Je vereenvoudigt het Windows-onderhoud door updates in te plannen in de Windows taakplanner en automatisch te installeren.

Wie de tijd neemt deze module te ontdekken en leren kennen kan PSWindowsUpdate gebruiken om updates in een netwerk op een vooraf ingepland tijdstip te installeren. Daarnaast kun je logging instellen om bij te houden of alle updates goed geïnstalleerd zijn.

Show 2 Comments

2 Comments

  1. You can use PowerShell to install Windows Updates automatically, unattended and simple. Neat, right? For this, you don’t have to have an enterprise environment with…

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *