PowerShell Script to Clean Up Empty SCCM Collections

Trever Sullivan wrote an excellent script to query your empty SCCM collections and prompts you to delete it.

It’s useful to delete bunch of old , empty collections.

Here is the script.

############################################### # # Author: Trevor Sullivan # Date: 12/6/10 # Purpose: Delete empty ConfigMgr collections # Blog: http://trevorsullivan.net # ############################################### # Your ConfigMgr server name (where the provider sites). Set this to your SCCM server name. $SccmServer = 'sccm01' # Create an array of excluded collections (add your own here) $ExcludedCollections = ,'COLLROOT' # Exclude these collection IDs (array) # Define the ConfigMgr provider namespace. Set the last three chars to your site code $SccmNamespace = 'rootsmssite_lab' # OK, house-keeping is out of the way. Now let's delete those collections! # Get a list of ALL collection objects $CollectionList = Get-WmiObject -Namespace $SccmNamespace -Class SMS_Collection -ComputerName $SccmServer # Iterate over [PowerShell] collection of [ConfigMgr] collections (haha ...) foreach ($Collection in $CollectionList) { # Define the WMI query language (WQL) query to get [ConfigMgr] collection members for the current [ConfigMgr] collection we're iterating over $WqlQuery = "select * from SMS_CollectionMember_a where collectionid = '" + $Collection.CollectionID + "'" # Execute the WQL query, pipe to Measure-Object, and get the Count property from the returned [PowerShell.Commands.GenericMeasureInfo] object $NumCollectionMembers = (Get-WmiObject -Query $WqlQuery -Namespace $SccmNamespace -ComputerName $SccmServer | Measure-Object).Count # If the number of collection members is equal to zero AND the exclusion list doesn't contain the collection ID, then go ahead with deletion if ($NumCollectionMembers -eq 0 -and $ExcludedCollections -notcontains $Collection.CollectionID) { # Let the user know what collection we're deleting (both ID and name) Write-Host ("Deleting collection ID (" + $Collection.CollectionID + ") named: " + $Collection.Name) # Prompt the user to delete the collection. They must type "yes" to delete each collection. if ((Read-Host -Prompt "Really delete? Type `"yes`"") -ieq "Yes") { # Delete the collection object $Collection.Delete() Write-Host "Deleted collection: " + $Collection.Name } } }

One thought on “PowerShell Script to Clean Up Empty SCCM Collections

  1. Great beat ! I wish to apprentice even as you amend your web site, how can i subscribe for a weblog site?
    The account helped me a applicable deal. I have
    been a little bit familiar of this your broadcast provided shiny clear idea

Leave a Reply

Your email address will not be published. Required fields are marked *

88 − = 87