Advertisement
nullzilla

Task - Install Windows Updates

Feb 11th, 2021 (edited)
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Set-ExecutionPolicy Unrestricted -Force
  2. Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
  3. Install-Module PSWindowsUpdate -Force
  4. Import-Module PSWindowsUpdate -Force
  5.  
  6. do {
  7.     # Retrieve list of available updates
  8.     "Checking for new updates..."
  9.     $updates = Get-WUList -Verbose -NotTitle Silverlight
  10.  
  11.     # Count how many updates are available
  12.     $updatenumber = ($updates.kb).count
  13.  
  14.     # If there are available updates proceed with installing the updates and then reboot
  15.     if ($null -ne $updates) {
  16.         Get-WindowsUpdate -AcceptAll -Install -AutoReboot -NotTitle Silverlight | Out-File $env:temp\PSWindowsUpdate.log
  17.         # Show update status until the amount of installed updates equals the same as the amount of updates available
  18.         Start-Sleep -Seconds 30
  19.         do {
  20.             $updatestatus = Get-Content $env:temp\PSWindowsUpdate.log
  21.             "Currently processing the following update:"
  22.             Get-Content $env:temp\PSWindowsUpdate.log | select-object -last 1
  23.             Start-Sleep -Seconds 10
  24.             $ErrorActionPreference = 'SilentlyContinue'
  25.             $installednumber = ([regex]::Matches($updatestatus, "Installed" )).count
  26.             $ErrorActionPreference = 'Continue'
  27.         }
  28.         until ( $installednumber -eq $updatenumber)
  29.     }
  30. }
  31. until($null -eq $updates)
  32.  
  33. # Update all Windows Store apps
  34. Get-CimInstance -Namespace "Root\cimv2\mdm\dmmap" -ClassName "MDM_EnterpriseModernAppManagement_AppManagement01" | Invoke-CimMethod -MethodName UpdateScanMethod
  35. Write-Output "Windows is now up to date, restarting"
  36. shutdown /r /f
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement