Guest User

Windows Update script

a guest
Jan 10th, 2025
2,217
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module PSWindowsUpdate
  2.  
  3. # Path to log file
  4. $LogPath = "C:\Scripts\Logs\Update-Windows-$(get-date -Format "yyyy-MM-dd HH-mm-ss").txt"
  5.  
  6. # Function to log messages
  7. function Write-Log {
  8.     param ([string]$Message)
  9.     Add-Content -Path $LogPath -Value "$Message" -Encoding unicode
  10. }
  11.  
  12. Write-Log "Starting Windows Update process."
  13.  
  14. try {
  15.     # Check for updates
  16.     $Updates = Get-WindowsUpdate -AcceptAll -IgnoreReboot
  17.  
  18.     if ($Updates) {
  19.         Write-Log "Updates found: $($Updates.Count). Installing updates."
  20.         Install-WindowsUpdate -AcceptAll -AutoReboot -Verbose | Tee-Object -FilePath $LogPath -Append
  21.         Write-Log "Updates installed successfully."
  22.     } else {
  23.         Write-Log "No updates available at this time."
  24.     }
  25. }
  26. catch {
  27.     Write-Log "Error during update process"
  28. }
Advertisement
Add Comment
Please, Sign In to add comment