Windows: Script to uninstall all Hotfixes and Updates

Hi,

here is a “oneliner” to uninstall all Windows Update at once. They were deinstalled in order from the highest to the lowest KB Number which increases speed of the remove process(Latest fix first).

PS C:\>  Get-WmiObject -query "Select HotFixID  from Win32_QuickFixengineering" | sort-object -Descending -Property HotFixID|%{
	$sUpdate=$_.HotFixID.Replace("KB","")
 	write-host ("Uninstalling update "+$sUpdate);
	& wusa.exe /uninstall /KB:$sUpdate /quiet /norestart;
	# while((Get-Process "wusa" -ErrorAction SilentlyContinue).Count -ge 1){start-sleep 1}
        # Waiting until an existing Hotfix installation finish. Improved by Paul T
        Wait-Process wusa
        Start-Sleep -s 1
}

And extended to deinstall a defined number of fixes

$iFixCount=20
Get-WmiObject -query "Select HotFixID  from Win32_QuickFixengineering" | sort-object -Descending -Property HotFixID|%{
	$sUpdate=$_.HotFixID.Replace("KB","")
 	write-host ("Uninstalling update "+$sUpdate);
	& wusa.exe /uninstall /KB:$sUpdate /quiet /norestart;
	# while((Get-Process "wusa" -ErrorAction SilentlyContinue).Count -ge 1){start-sleep 1}
        # Waiting until an existing Hotfix installation finish. Improved by Paul T
        Wait-Process wusa
        Start-Sleep -s 1
	$iFixCount--
	if($iFixCount -le 0){
		break
	}
 }
shutdown /t 0 /r /f

Have fun

Michael

4 thoughts on “Windows: Script to uninstall all Hotfixes and Updates”

  1. Thanks for this.

    The first script to uninstall all updates didn’t quite work for me. It didn’t wait for the wusa process to finish and carried on trying to do the others while one was already running.

    I fixed this by just removing the line:
    while((Get-Process “wusa” -ErrorAction SilentlyContinue).Count -ge 1){start-sleep 1}

    and adding in the following 2 lines in its place:
    Wait-Process wusa
    Start-Sleep -s 1

    So the complete script that worked for me as expected was:

    Get-WmiObject -query “Select HotFixID from Win32_QuickFixengineering” | sort-object -Descending -Property HotFixID|%{
    $sUpdate=$_.HotFixID.Replace(“KB”,””)
    write-host (“Uninstalling update “+$sUpdate);
    & wusa.exe /uninstall /KB:$sUpdate /quiet /norestart;
    Wait-Process wusa
    Start-Sleep -s 1
    }

  2. Thanks for this nice article.

    Just a suggestion:
    (Select-Object -First 20) can also be used to uninstall limited number of KBs.

    With your original code:

    Get-WmiObject -query “Select HotFixID from Win32_QuickFixengineering” | sort-object -Descending -Property HotFixID | Select-Object -First 20|%{
    $sUpdate=$_.HotFixID.Replace(“KB”,””)
    write-host (“Uninstalling update “+$sUpdate);
    & wusa.exe /uninstall /KB:$sUpdate /quiet /norestart;
    # while((Get-Process “wusa” -ErrorAction SilentlyContinue).Count -ge 1){start-sleep 1}
    # Waiting until an existing Hotfix installation finish. Improved by Paul T
    Wait-Process wusa
    Start-Sleep -s 1
    }

Leave a Reply