Automating the clean-up of Configuration Manager Maintenance Collections


Most organizations using System Center Configuration Manager implement collections configured for maintenance tasks. Administrators generally monitor these collections on a weekly/monthly schedule and in some instances are required to delete the machines within these collections, for example: collections containing Obsolete Clients.


My customer was looking for a method to streamline their weekly/monthly maintenance tasks where they manually delete machines from multiple collections.


Using System Center Configuration Manager PowerShell cmdlets a script was created to:
•    Read an input text file that is populated with specific collection names.

•    Automatically delete all machines from the collections specified in the input text file.
Note, use this script with extreme caution as machines are deleted from the SCCM database therefore always ensure the correct collection names are populated in the input text file. Test this script in your lab environment to ensure it works as desired.

•    Invoke a collection membership update once machines were deleted.

•    Output a logfile that records the collection names and the machines that were deleted, respectively.


PowerShell Script:

#           This script performs the following actions:
#            - CAUTION, deletes machines from SCCM in specified collections
#            - Updates the collection membership
#            - Creates a Logfile with Date, Time, Collection Name and Machine Names
#           This script does NOT:
#            - Remove the collection rulesquery

# Load Configuration Manager PowerShell Module
Import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + 'ConfigurationManager.psd1')

# Get SiteCode
$SiteCode = Get-PSDrive -PSProvider CMSITE
Set-location $SiteCode":"

# Define Input File
$script_parent = Split-Path -Parent $MyInvocation.MyCommand.Definition
$inputfile = $script_parent + "InputFile.txt"
$list = get-content $inputfile

# Define Logfile Parameters
# Logfile time stamp
$LogTime = Get-Date -Format "dd-MM-yyyy_hh-mm-ss"
# Logfile name
$Logfile = $script_parent + "CMRemoveDevice_"+$LogTime+".log"
Function LogWrite
Param ([string]$logstring)
Add-content $Logfile -value $logstring

# Remove machines in a collection from SCCM
ForEach ($CollectionName In $list)
Foreach ($CMDevice in $CollectionName)
$CMDevice = Get-CMDevice -CollectionName $CollectionName | Format-List -Property Name | Out-String
Get-CMDevice -CollectionName $CollectionName | Remove-CMDevice -Force -ErrorAction SilentlyContinue
Get-CMDeviceCollection -Name $CollectionName | Invoke-CMCollectionUpdate -ErrorAction SilentlyContinue
LogWrite "$LogTime | $CollectionName | $CMDevice"


Expected Log File Output:



System Center Configuration Manager is a product that has a broad offering of features. Administrators at times can be overwhelmed by operational activities and overlook monitoring of maintenance collections or performing maintenance tasks. Automating of tasks to run on a regular schedule can provide consistency in maintaining the health of your environment. The above script can be configured to run using Task Scheduler.


Hope the shared script is helpful in maintaining your environment.


Disclaimer – All scripts and reports are provided ‘AS IS’
This sample script is not supported under any Microsoft standard support program or service. This sample script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of this sample script and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of this script be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use this sample script or documentation, even if Microsoft has been advised of the possibility of such damages.