Did you ever wanted to delete a bunch of collections older than x days under specific folder name?

I recently needed to clean collections older than 7 days under specific folders. The reason was simple, I came across a situation where the company own a custom package deployment solution and it create a collection each time a deployment is made. Since they are all limited collections, you can imagine the load it can create for the collection evaluator. The best solution would be to delete the collection as soon the deployment is succeeded, some internal politics prevents us to do that.

Instead of doing it one by one, I decided to create an automate PowerShell script that run each week.

The PowerShell script create a small report in a text file including the list of collections ID deleted.

The file will be sent by email as an attachment and will be copied to a specified location.

The file name contains the date so the script can be used on a daily basic and keep history.


Please make sure to modify the variables in the PowerShell script according to your infrastructure.

$SiteCode = Site code of your configuration manager infrastructure. (Default = CAS)
$SiteServer = The name of the site server of configuration manager.
$FolderName = The folder name you want to delete. You can use the wildcard % if it is more than one folder.
$Days = The number of days older you want to delete.
$FileDestination = The directory where you want to put the report file.
$EmailTo = The email address to who you want to send the report.
$EmailFrom = The email address from you want to send the report.
$SmtpServer = The SMTP server you want to use to send the report.

The PowerShell script can be downloaded from my Microsoft Gallery submission using this link: http://gallery.technet.microsoft.com/Delete-all-collections-0e935968

You can also verify the collections you want to delete with the report Configuration Manager – Collections.

Powershell script to delete older collections in a folder in SCCM

Please contact us for any issue and be sure to rate the submission if you are using it. 

Comments (1)


02.28.2019 AT 08:46 AM
Hi All, Could you please help with a power shell script that will check for empty folders (folders without collections/deployments in it) in SCCM console and delete it ? I tried blogging around and could not see one for it. Thank you Praveen