CBC Computer Services

PowerShell Script to Uninstall Recent Updates

If you need to uninstall recent Windows updates, you can use PowerShell to manage this task. Here’s a script that helps you find and uninstall recent updates. This script uses the Get-HotFix cmdlet to list installed updates and the wusa.exe command to uninstall specific updates.

PowerShell Script to Uninstall Recent Updates

# List recent updates
Write-Host "Retrieving recent updates..."
$updates = Get-HotFix | Sort-Object -Property InstalledOn -Descending

# Display recent updates
Write-Host "Recent updates:"
$updates | Format-Table -Property Description, InstalledOn, HotFixID -AutoSize

# Prompt user to enter the HotFixID of the update to uninstall
$updateToRemove = Read-Host "Enter the HotFixID of the update you want to uninstall (e.g., KB5005565)"

if ($updateToRemove) {
    Write-Host "Attempting to uninstall update $updateToRemove..."

    # Construct the command to uninstall the update
    $uninstallCommand = "wusa.exe /uninstall /kb:$updateToRemove /quiet /norestart"

    # Execute the command
    Start-Process -FilePath "cmd.exe" -ArgumentList "/c $uninstallCommand" -NoNewWindow -Wait

    Write-Host "Uninstall command executed. Please restart your computer to complete the process."
} else {
    Write-Host "No update specified. Exiting script."
}

How to Use This Script:

  1. Open PowerShell as Administrator:
  • Right-click the Start button and select Windows PowerShell (Admin).
  1. Save the Script:
  • Open Notepad or another text editor and paste the script above.
  • Save the file with a .ps1 extension, e.g., UninstallRecentUpdates.ps1.
  1. Run the Script:
  • Navigate to the directory where you saved the script using PowerShell.
  • Execute the script by typing:
    powershell .\UninstallRecentUpdates.ps1
  1. Follow Prompts:
  • The script will display recent updates and prompt you to enter the HotFixID of the update you wish to uninstall. Enter the ID (e.g., KB5005565), and the script will run the uninstallation command.

Explanation of Script Components:

  • Get-HotFix: Retrieves a list of installed updates and hotfixes.
  • Format-Table: Formats the output to show update description, installation date, and HotFixID.
  • wusa.exe /uninstall: The command-line utility to uninstall Windows updates.
  • Start-Process: Runs the wusa.exe command to uninstall the specified update.

Notes:

  • Be Cautious: Uninstalling updates can affect system stability or security. Ensure that the update you are uninstalling is causing issues and that removing it is a suitable solution.
  • Restart Required: A restart is generally required after uninstalling updates for changes to take effect.

By using this script, you can manage recent updates more efficiently, especially in scenarios where updates cause system issues or conflicts.