Trevor Sullivan's Tech Room

Minding the gap between administration and development

PowerShell: Cleaning Up Empty ConfigMgr Collections

Posted by Trevor Sullivan on 2010/12/06


Microsoft System Center 50%Someone recently posted on the MyITforum ConfigMgr mailing list, asking how to delete a bunch of old, empty collections in ConfigMgr. I took this opportunity to write a simple PowerShell script that will do just that. The code simply iterates over all collections, looks to see if each collection has members, and if not, then it prompts the user to delete it.

You’ll need to configure a couple things at the top of the script, before you run it:

  • ConfigMgr server name ($SccmServer)
  • ConfigMgr provider namespace (just replace the last 3 characters with your site code)
  • Add any collection IDs to the $ExcludedCollections array, that you want to explicitly exclude

If you’re really daring, and are absolutely confident that you want to remove all empty collections, you can remove the user confirmation. I’ll leave that up to you, if you’re savvy enough 🙂

Here’s a screenshot of the script running in Quest’s free PowerGUI scripting environment:

image

Here’s the code, with comments in-line:

############################################### # # 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='root\sms\site_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-ClassSMS_Collection-ComputerName$SccmServer# Iterate over [PowerShell] collection of [ConfigMgr] collections (haha ...)foreach ($Collectionin$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 deletionif ($NumCollectionMembers-eq0-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 } } }
Advertisements

5 Responses to “PowerShell: Cleaning Up Empty ConfigMgr Collections”

  1. […] Read the full post and get the script here. […]

  2. PowerShell: Cleaning Up Empty ConfigMgr Collections…

    Nice post by Trevor Sullivan. Read the full post and get th ……

  3. […] plus d’informations, lire PowerShell: Cleaning Up Empty ConfigMgr Collections Published 12-09-2010 9:28 by Jean-Sébastien DUCHÊNE Filed under: SCCM, System Center […]

  4. Kapil said

    Sorry, I am not able to find where the code is? I don’t see any link on any of below line:

    Here’s the code, with comments in-line:
    ############################################### # # Author: Trevor Sullivan

    • Trevor Sullivan said

      Looks like a problem with the line wrapping … not sure what caused that. I’ll have to fix it when I can get to my computer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: