Blog featured Image

Deleting-Sccm-Collections-Guide

Loading

 

Author: Nawaz kazi

Managing large IT environments often involves the challenge of ensuring that your System Center Configuration Manager (SCCM) is neat, efficient, and free of clutter. One common task administrators face is the deletion of outdated SCCM collections. In this blog, we’ll guide you through a script that simplifies this process.

What Does This Script Do?

This PowerShell script aims to:

  1. Read a list of SCCM collections from a CSV file.
  2. Verify their existence.
  3. Identify their type (User or Device).
  4. Attempt to delete them.
  5. Record the results in an Excel spreadsheet.

This ensures a streamlined, error-free, and documented process of cleaning up your SCCM environment.

Getting Started

Before you dive in, ensure you have:

  • Admin rights to the SCCM.
  • The required SCCM module to interact with it via PowerShell.
  • A CSV file with the names of collections you wish to delete.

Delving into the Script

Initializing:

The script loads the SCCM module and dynamically determines the SCCM site code, providing flexibility across different environments.

Import-Module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1')
$siteCode = (Get-WmiObject -Namespace "root\SMS" -Class SMS_ProviderLocation).SiteCode
cd "$siteCode:\"

Preparing the Excel Output:

The system initializes an instance of the Excel application and defines and formats the headers.

# Initialize an instance of the Excel application and create a new workbook for output
$excelApp = New-Object -comobject Excel.Application
$excelApp.visible = $True
$workbook = $excelApp.Workbooks.Add()
$worksheet = $workbook.Worksheets.Item(1)

# Define and format the headers for the Excel worksheet
$worksheet.Cells.Item(1, 1) = "Collection Name"
$worksheet.Cells.Item(1, 2) = "Collection Type"
$worksheet.Cells.Item(1, 3) = "Status"

# Format the header row with colors and bold text
$usedRange = $worksheet.UsedRange
$usedRange.Interior.ColorIndex = 19
$usedRange.Font.ColorIndex = 11
$usedRange.Font.Bold = $True

# Initialize the starting row for data population
$row = 2

Reading and Processing Collections:

The script reads collection names from the CSV file you specify and processes each one.

$collections = Import-Csv -Path "C:\path\to\your\input.csv" | ForEach-Object { $_.CollectionName }

The script checks each collection to see if SCCM contains it. If SCCM does contain it, the script fills in the details in the Excel sheet and attempts to delete it based on the collection type.

# Loop through and process each collection name
foreach ($collectionName in $collections) {
    # Ensure collection names are not empty or null
    if (![string]::IsNullOrEmpty($collectionName)) {
        try {
            # Query SCCM to see if the specified collection exists
            $collection = Get-CMCollection -Name $collectionName -ErrorAction Stop

            # If the collection exists, populate its details in the Excel sheet
            if ($collection) {
                $worksheet.Cells.Item($row, 1) = $collectionName
                $worksheet.Cells.Item($row, 2) = if ($collection.CollectionType -eq '1') { "User Collection" } else { "Device Collection" }
                
                # Attempt to delete the collection based on its type and record the status in Excel
                switch ($collection.CollectionType) {
                    '1' { 
                        Remove-CMUserCollection -Name $collectionName -Force -ErrorAction Stop
                        $worksheet.Cells.Item($row, 3) = "Deleted"
                    }
                    '2' { 
                        Remove-CMDeviceCollection -Name $collectionName -Force -ErrorAction Stop
                        $worksheet.Cells.Item($row, 3) = "Deleted"
                    }
                    default { 
                        $worksheet.Cells.Item($row, 3) = "Unknown collection type"
                    }
                }
            } else {
                # Record in Excel when a collection does not exist in SCCM
                $worksheet.Cells.Item($row, 1) = $collectionName
                $worksheet.Cells.Item($row, 2) = "N/A"
                $worksheet.Cells.Item($row, 3) = "Collection not found"
            }

Error Handling:

The script is designed to handle exceptions gracefully. The system captures and records this specific error in Excel if it cannot delete a collection due to it being a referenced collection.

catch {
            # Handle any exceptions and record the error details in Excel
            $errorMessage = "$_"
             if ($errorMessage -match "ErrorCode = 3242722566;") {
                $errorMessage = "This collection cannot be deleted as it's acting as a referenced collection"
            }
            $worksheet.Cells.Item($row, 1) = $collectionName
            $worksheet.Cells.Item($row, 2) = "Error"
            $worksheet.Cells.Item($row, 3) = $errorMessage
        }
        # Move to the next row in preparation for the next collection
        $row++
    }
}

Full Script :

# SCCM Collection Deletion Script
# This script reads a list of SCCM collections from a CSV file, checks if they exist, determines their type (User or Device),
# and attempts to delete them. The results are then written to an Excel spreadsheet.

# Load the SCCM module to interact with SCCM via PowerShell
Import-Module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1')

# Determine the SCCM site code dynamically
$siteCode = (Get-WmiObject -Namespace "root\SMS" -Class SMS_ProviderLocation).SiteCode

# Navigate to the SCCM site directory using the dynamically determined site code
cd "$siteCode:\"

# Initialize an instance of the Excel application and create a new workbook for output
$excelApp = New-Object -comobject Excel.Application
$excelApp.visible = $True
$workbook = $excelApp.Workbooks.Add()
$worksheet = $workbook.Worksheets.Item(1)

# Define and format the headers for the Excel worksheet
$worksheet.Cells.Item(1, 1) = "Collection Name"
$worksheet.Cells.Item(1, 2) = "Collection Type"
$worksheet.Cells.Item(1, 3) = "Status"

# Format the header row with colors and bold text
$usedRange = $worksheet.UsedRange
$usedRange.Interior.ColorIndex = 19
$usedRange.Font.ColorIndex = 11
$usedRange.Font.Bold = $True

# Initialize the starting row for data population
$row = 2

# Read collection names from the specified CSV file
$collections = Import-Csv -Path "C:\path\to\your\input.csv" | ForEach-Object { $_.CollectionName }

# Loop through and process each collection name
foreach ($collectionName in $collections) {
    # Ensure collection names are not empty or null
    if (![string]::IsNullOrEmpty($collectionName)) {
        try {
            # Query SCCM to see if the specified collection exists
            $collection = Get-CMCollection -Name $collectionName -ErrorAction Stop

            # If the collection exists, populate its details in the Excel sheet
            if ($collection) {
                $worksheet.Cells.Item($row, 1) = $collectionName
                $worksheet.Cells.Item($row, 2) = if ($collection.CollectionType -eq '1') { "User Collection" } else { "Device Collection" }
                
                # Attempt to delete the collection based on its type and record the status in Excel
                switch ($collection.CollectionType) {
                    '1' { 
                        Remove-CMUserCollection -Name $collectionName -Force -ErrorAction Stop
                        $worksheet.Cells.Item($row, 3) = "Deleted"
                    }
                    '2' { 
                        Remove-CMDeviceCollection -Name $collectionName -Force -ErrorAction Stop
                        $worksheet.Cells.Item($row, 3) = "Deleted"
                    }
                    default { 
                        $worksheet.Cells.Item($row, 3) = "Unknown collection type"
                    }
                }
            } else {
                # Record in Excel when a collection does not exist in SCCM
                $worksheet.Cells.Item($row, 1) = $collectionName
                $worksheet.Cells.Item($row, 2) = "N/A"
                $worksheet.Cells.Item($row, 3) = "Collection not found"
            }
        } catch {
            # Handle any exceptions and record the error details in Excel
            $errorMessage = "$_"
             if ($errorMessage -match "ErrorCode = 3242722566;") {
                $errorMessage = "This collection cannot be deleted as it's acting as a referenced collection"
            }
            $worksheet.Cells.Item($row, 1) = $collectionName
            $worksheet.Cells.Item($row, 2) = "Error"
            $worksheet.Cells.Item($row, 3) = $errorMessage
        }
        # Move to the next row in preparation for the next collection
        $row++
    }
}

# Adjust the Excel column widths to fit the content
$usedRange.EntireColumn.AutoFit()

Conclusion:

This script offers a systematic way to declutter your SCCM by removing outdated collections. Not only does it provide automation, but it also ensures that you have a record of every action performed. So, the next time you’re faced with the daunting task of cleaning up your SCCM, let this script do the heavy lifting for you.

1 thought on “Deleting-Sccm-Collections-Guide”

Leave a Comment

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