Tuesday, December 1, 2015

Remove Windows Update using PowerShell and KB

I would like a native cmdlet in PowerShell to remove Windows Updates.

Until that time comes, I've created my own function that takes the native comobjects and searches for the update I want to remove by KB ID.

Bon App├ętit

Current version at https://github.com/cajeeper/PowerShell/blob/master/Remove-WindowsUpdate.ps1

  Remove One to Many Windows Updates
  Remove One to Many Windows Updates from OS.
  Author   : Justin Bennett   
  Date     : 2015-12-01  
  Contact  : http://www.allthingstechie.net
  Revision : v1  
  C:\PS> #Uninstall One Update 
  C:\PS> Remove-WindowsUpdate 123456
  C:\PS> #Uninstall Multiple Updates
  C:\PS> Remove-WindowsUpdate 123456,456123
Function Remove-WindowsUpdate {
      param (  
           [parameter(Mandatory=$True)] $RemoveKB

 $Searcher = New-Object -ComObject Microsoft.Update.Searcher
 $RemoveCollection = New-Object -ComObject Microsoft.Update.UpdateColl

 #Gather All Installed Updates
 $SearchResult = $Searcher.Search("IsInstalled=1")

 #Add any of the specified KBs to the RemoveCollection
 $SearchResult.Updates | ? { $_.KBArticleIDs -in $RemoveKB } | % { $RemoveCollection.Add($_) }

 if ($RemoveCollection.Count -gt 0) {
  $Installer = New-Object -ComObject Microsoft.Update.Installer
  $Installer.Updates = $RemoveCollection
 } else { Write-Warning "No matching Windows Updates found for:`n$($RemoveKB|Out-String)" }
Formatted for web with http://codeformatter.blogspot.com/ 

No comments: