SCCM Configmgr 2012 Powershell script cleanup duplicate obsolete and outdated computers Via Task Scheduler

This post is continuation to my previous post on how to clean outdated computers from configmgr Database. More info refer ,

Through this post,i will show you to create Powershell script cleanup duplicate obsolete and outdated computers Via Task schedule that deletes the computers from specific collections to maintain configmgr database healthy.

In my previous blog,we have seen how to create collections for duplicate,obsolete,computers not contacted to domain during X days with hardware inventory older than 30 days etc.

Note: if you are imaging the computers frequently ,you may see many duplicate/obsolete computers are you are required to cleanup those entries at regular intervals.

Creating collections is nice but how frequent do you clean these entries ? keep checking once in week or once in month to delete the entries from database to have healthy environment and high success ratio on client health when your boss ask for client health statistics?

There are configmgr Maintenance tasks that does the cleanup job for you like delete inactive,aged inventory data etc based on what task you enable. More about Maintenance tasks,refer and default settings for maintenance tasks

Through this post,i will show how to create powershell script to do the removal of computer records from specific collections (you can have many collections that contains out dated computers with different criteria) and empty the collection on scheduled basis.


I have captured few lines of code from David blog to get site code,site server name etc instead of providing them manually.

Download the script from TechNet Gallery

Script function: It takes the collectionIDs as input parameter what you supply while running the script,check if the collection Exist ,check if the collection is not empty  and then perform removal of computer Objects.

Before removal of computer objects ,script will write computer objects to txt file for reference incase you need check later.

After you download the script,put it in either share folder or on local server.

PS:You don't have to make any changes to the script ,just get the collection's which you want to cleanup.

Create Task scheduler with recurring weekly or monthly at your convenience,get help from .

After you create Task scheduler,open the properties of Task and go to Action Tab,select the Actions ,click on Edit.


For program,/Script: type PowerShell.exe ,and for Arguments (Optional): type -command "& 'G:\SCCM Tools\delete-resources-from-collection.ps1' 'PRI0001E' ,'PRI00020'

where 'G:\SCCM Tools\delete-resources-from-collection.ps1: is powershell script what we created earlier and PRI0001E,PRI00020 are the collections that i have to cleanup. If you have more collections ,just append the existing command like with ,’collectionID’.

Actions Tab:


Make sure you run the task with right permissions on the database .you can change the account name to run the task from General Tab:


run the task,wait until it finishes. Go back your script location,you will see .txt file created with script ran date:


The txt file contains list of computers which are removed from the specific collection.

Hope if helps for someone who wants to keep database active without outdated Clients ;).

If you want to know what causes the client become Inactive ,refer

4 Responses to "SCCM Configmgr 2012 Powershell script cleanup duplicate obsolete and outdated computers Via Task Scheduler"

  1. Have you ran the script to delete computers from x Collection recently? Trying to figure out why it's not working for me.

    "PS D:\Scripts> .\Delete_Computers_From_Collection.ps1 nt1000a2
    Method invocation failed because [System.Management.Automation.PSInternalMemberSet] does not contain a method named
    At D:\Scripts\Delete_Computers_From_Collection.ps1:63 char:10
    + $comp.psbase.delete()
    + ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (delete:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound"


Leave a Reply