Automating Windows Updates with PowerShell

Introduction

Keeping Windows up-to-date is crucial for system security and performance. PowerShell can automate the process of managing and applying Windows updates. This guide will cover how to check for updates, install updates, and manage update settings using PowerShell.

Checking for Updates

The following script checks for available updates on your system.

Install-Module PSWindowsUpdate -Force; Import-Module PSWindowsUpdate; Get-WindowsUpdate

Explanation: This script first installs the `PSWindowsUpdate` module, which provides cmdlets for managing Windows updates. The `Get-WindowsUpdate` cmdlet then checks for available updates. This is useful for identifying which updates need to be applied.

Installing Updates

Once updates are detected, you can install them using the following script.

Install-Module PSWindowsUpdate -Force; Import-Module PSWindowsUpdate; Install-WindowsUpdate -AcceptAll -AutoReboot

Explanation: This script installs the `PSWindowsUpdate` module and imports it. The `Install-WindowsUpdate` cmdlet is then used to install all available updates. The `-AcceptAll` parameter accepts all updates, and `-AutoReboot` automatically restarts the system if necessary, ensuring that the updates are fully applied.

Checking Update History

To view the history of installed updates, use the following script.

Get-HotFix

Explanation: The `Get-HotFix` cmdlet retrieves a list of installed updates and hotfixes on the system. This can be useful for verifying that updates have been applied successfully or for troubleshooting update-related issues.

Managing Update Settings

To configure Windows Update settings, you can use the following script.

Invoke-WebRequest -Uri "https://support.microsoft.com/en-us/help/4027667/windows-10-windows-update-troubleshooter" -OutFile "WindowsUpdateTroubleshooter.exe"; Start-Process -FilePath "WindowsUpdateTroubleshooter.exe"

Explanation: This script downloads the Windows Update Troubleshooter tool and executes it. The `Invoke-WebRequest` cmdlet downloads the troubleshooter from Microsoft’s support website and saves it as `WindowsUpdateTroubleshooter.exe`. The `Start-Process` cmdlet then runs the troubleshooter, which can help diagnose and resolve common issues related to Windows Update settings. This is particularly useful for troubleshooting problems with the update process that might not be resolved through standard PowerShell commands.

Resources