Wednesday, 12 March 2014

SCCM 2012 R2 - Scripted removal of expired or superseded updates with Powershell

Hi All,

Unfortunately SCCM 2012 still has a requirement to manually remove expired and superseded updates. Quite sometime ago I found and modified Trevor Sullivan's Powershell script for use with SCCM 2007. As I have just build a new SCCM 2012 R2 environment for a client I decided I would spend the time to update the script for use with SCCM 2012.

Note: By removing superseded updates via this script I would assume that the newer superseding updates are being included in new deployments.

Original script source:
Trevor Sullivan's Blog

New and Updated Features:
  • Updated the script to a parameterized advanced Powershell script.
  • Added Powershell Help functionality.
  • Updated to enable targeting of remote primary site servers.
  • Updated to only trigger a DP refresh for packages which have had updates removed.
  • Commented out the 'SMS_UpdatesAssignment' (Deployments) code section as updates listed in this WMI class now appear to automatically mirror the updates contained in the associated 'Software Update Group' so do not need to reviewed\updated.
  • Added progress bars.
  • Added  the use of Powershell transcripts for basic logging.
  • Tested against SCCM 2012 R2
Actions Performed:
  • Removal of Expired or Superseded updates from:
    • Software Update Groups
    • Software updates packages
  • Distribution Point refresh is triggered for packages which have been modified.

.\Remove-SCCMExpiredSupersededContent_1.0.ps1 [PrimarySiteServerName]

Note: If no server name is specified you will be prompted to provide one.



  1. Hello Ben!

    I was quite happy to find that you had refreshed Trevor's script as I was using it with my soon-to-be expired 2007 environment. We are currently in the process of rolling out 2012 R2 as well, so you can imagine my joy at seeing that you not only reused a script I knew already about, but also tested it on a 2012R2 environment.

    However, I seem to be running into some issues. If you wish, I can send you a private email with the full log file, but it seems to be looping on the following error message:

    VERBOSE: Removing content ID 16811494 from package CDP00014
    Exception calling "RemoveContent" : "Generic failure "
    At E:\SCCM\Scripts\Remove-SCCMExpiredSupersededContent_1.0.ps1:198 char:21
    + If ($SoftwareUpdatesPackage.RemoveContent($ExpiredContent.Conten ...

    Any idea what the problem could be?

    I'll keep on troubleshooting in the meantime!



  2. Hi Stephanie,

    Since posting I too have noticed an issue with grooming a package which is populated with EP virus definitions by an auto deployment rule. I have been distracted by other work but will try to troubleshoot further tomorrow. Feel free to send me an email directly so we can work together to nut out what is hopefully a common and simple issue.



    1. Done! Hopefully we can find a solution quickly and post back our findings here :)

    2. I have the same issue with multiple content IDs from multiple packages ...

  3. Does this only work with SCCM 2012R2 or would it work with SCCM2012?

  4. Hi guys,

    I just stumble upon this blog as i was looking for a solution to automate update removal in SCCM and this script looks like it could do the trick.

    I was wondering you had a chance to look at the errors and maybe even found the solution to this? I just ran the script on my side and seen slightly different error as is point out to line 198 but Char:58.

    Exception calling "RemoveContent" : "Generic failure "
    At C:\scripts\Remove-SCCMExpiredSupersededContent_1.0.ps1:198 char:58
    + If ($SoftwareUpdatesPackage.RemoveContent <<<< ($ExpiredContent.ContentID, $false).ReturnValue -eq 0)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

    I'll start looking it up on my sie but if one of you already found the solution i'd apreciate an update. Will keep you posted if i find anything on my side.



  5. Hi Guys,

    My apologies for not being able to find time to progress the script but unfortunately work has been pretty hectic as of late. From memory the issue is around how EndPoint updates are referenced in packages I only encountered issues with packages which contain EP updates. One temporary workaround would be to have a dedicated package for EP updates and then modify the script to skip taking any action on that PackageID. As soon as I get some free time I will try to look into this further myself. If anyone has other suggestions then feel free to reply.



  6. Ben,


    Unfortunately for me, I too face the 'general failure' error due to EP updates being managed via Automatic Deployment Rules.

    I don't get the opportunity to spend as much time in SCCM as I would like so I look forward to seeing an update to the script when you find some time to revisit it.


  7. Hi,
    very nice work! I'm currently working on a SCCM-Helper GUI. Is it ok with you to take your script as a part? When it's finished, I will upload it to the technet gallery.
    I would change the header of the script, so that your name is always shown when someone runs the script!


  8. Hello,
    Very nice script, I'm using it on my SCCM 2012 R2 with good results.
    However, the error "Generic failure" while cleaning some FEP updates still plagues my logs. Did you find what is causing this?


  9. Hi,
    I fixed the Generic failure error by amending the SQL queries:

    Added DISTINCT to the first query, changed %$SCCMSiteCode% to $SCCMSiteCode% and added brackets to the where clauses.

  10. Thought I'd try this on SCCM 2016...

    It removed quite a bit, but it still gets stuck and won't remove some. Here is the error message that keeps appearing for each "content ID":

    VERBOSE: Removing content ID 16787561 from package G0100009
    Exception calling "RemoveContent" : "Generic failure "
    At C:\Remove-SCCMExpiredSupersededContent_1.0.ps1:198 char:21
    + ... If ($SoftwareUpdatesPackage.RemoveContent($ExpiredContent.Con ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

    Any ideas?