Powershell – Script to Uninstall MS Updates by Date

powershellvbscriptwindows-server-2003

I'm in a situation where I need to uninstall all windows updates before a certain date to troubleshoot a strange bug. I found several vb/ps scripts to do this

remove single update

However I'm trying to get at something along the lines of where date > 1/9/13 get rid of it !

The reason I ask is I feel like this is something I end up running into from time to time and ends up being pretty time consuming. Any ideas ?

I also found the following

wmic qfe get hotfixid,installedon | findstr %01/09/13% >> c:\list.txt

However this doesn't seem to return an accurate result set

I'm currently working with a windows server 2003 box. Thanks in advance.

Best Answer

Here's one way to filter hotfixes by date using VBScript. You could easily convert it to PowerShell if needed or just call your PowerShell code to remove each hotfix that matches the filter.

On Error Resume Next
Dim strComputer
Dim objWMIService
Dim propValue
Dim objItem
Dim SWBemlocator
Dim UserName
Dim Password
Dim colItems

Dim dateFilter

dateFilter = CDate("1/9/2013")

strComputer = "."
UserName = ""
Password = ""
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",UserName,Password)
Set colItems = objWMIService.ExecQuery("Select * from Win32_QuickFixEngineering",,48)
For Each objItem in colItems
    hotFixID = objItem.HotFixID
    installDate = objItem.InstallDate

    If IsNull(installDate) Or Trim(installDate) = "" Then
        installDate = objItem.InstalledOn
    End If

    If IsDate(installDate) Then
        If CDate(installDate) > dateFilter Then
            WScript.Echo hotFixID & vbTab & installDate

            ' Call your remove hotfix script or code and pass it the hotFixID and optionally, the machine name
        End If
    End If
Next