PowerShell: Cleaning Up Empty ConfigMgr Collections

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='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-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 } } }

PowerShell: PowerEvents Module

Hey guys, I haven’t written anything new in a while, because I’ve been working on a PowerShell module called PowerEvents. PowerEvents is a module that facilitates working with WMI (Windows Management Instrumentation) permanent event registrations. You can query for and respond to events, all from within WMI! PowerEvents simply makes creating those registrations easier.

For more information about this module, please visit the project page on CodePlex.

Look for a couple articles on the Hey, Scripting Guy! blog (@ScriptingGuys) next week, as well as my appearance on the @PowerScripting podcast with Hal Rottenberg and Jonathan Walz! Thanks for the invitation, Hal!