SCCM have pool of collections.These gets created based on the requirement. and this pool gets bigger and bigger when time passes. There may be very old collections which are not of use anymore. Hoever from pool of many collections , its difficult to find the collections where there is no deployments or no member inside the collection. Checking one by one each collection is going to take huge time.

This script is going to automate the same. Script will connect to SCCM and perform the below activity:

  1. Collect list of collections with required parameters like name,collectionid,membercount etc.
  2. Then it will list all the deployment and from them it will extract the collections
  3. Script will compare both the list and extract the collections which are not present in deployment collections list.
  4. Export those list to HTML Page.



function InitializeSCCM  
    $ProcessMessage="`n Please wait.Initializing SCCM ........."  
    # Site configuration. Please provide site code and sms server information 
                write-host "`n Enter Site Code : " -foregroundcolor $inputcolor -nonewline  
                $SiteCode = read-host  
                $siteResult=($siteCode -match '\b^[a-zA-Z0-9]{3}\b') 
                write-host " Site code can have only [3] alphanumeric characters. Please re-enter site code" -foregroundcolor RED 
                write-host "`n Enter SMS Provider Server Name : " -foregroundcolor $inputcolor -nonewline  
                $ProviderMachineName = read-host  
                $nameResult=($ProviderMachineName -match '\b^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$\b') 
                write-host " Entered SMS provider name is not valid as per naming conventions. Please re-enter provider name" -foregroundcolor RED 
    iex $ProcessColor  
    sleep 2  
    # Customizations  
    $initParams = @{}  
    # Import the ConfigurationManager.psd1 module   
    if((Get-Module ConfigurationManager) -eq $null) {  
        Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams   
    # Connect to the site's drive if it is not already present  
    if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {  
        New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams  
    # Set the current location to be the site code.  
    Set-Location "$($SiteCode):\" @initParams  
function deinitializeSCCM  
    $ProcessMessage="`n Please wait.De-Initializing SCCM ......"  
    iex $ProcessColor  
    sleep 2  
    set-location $location  
function updateHTML 
param ($strPathIF(Test-Path $strPath){Remove-Item $strPath} 
#--CSS formatting 
<style type="text/css"> 
 h1, h5,h2, th { text-align: left; font-family: Segoe UI;font-size: 13px;} 
table { margin: left; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; } 
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; font-size: 12px;} 
td { font-size: 11px; padding: 5px 20px; color: #000; } 
tr { background: #b8d1f3; } 
tr:nth-child(even) { background: #dae5f4; } 
tr:nth-child(odd) { background: #b8d1f3; } 
#--Variable declaration 
 $ProcessColor="write-host `$ProcessMessage -ForegroundColor gray -BackgroundColor darkgreen"  
 $strPath = "$location\$ReportTitle.html"  
        $COLLECTION_RESULT = @() 
        write-host "Getting entire collection list" -foregroundcolor $inputcolor 
        $COLLECTION_LIST = Get-CmCollection | Where-Object {$_.CollectionID -notlike 'SMS*' -and $_.CollectionType -eq '2'| Select Name,MemberCount,CollectionID,IsReferenceCollection,LastMemberChangeTime 
        write-host "Collection list gathering completed." -foregroundcolor $inputcolor 
        write-host "Getting entire deployment list" -foregroundcolor $inputcolor 
        write-host "Deployment list gathering completed." -foregroundcolor $inputcolor     
        $COMPARED_RESULT=(Compare-Object $DEPLOYMENT_List $COLLECTION_LIST.collectionid |? {$_.sideindicator -eq "=>"}).inputobject 
        write-host "I am looking for collections having member count less than OR equal to=" -foregroundcolor $inputcolor -nonewline 
        foreach($c in $COMPARED_RESULT) 
                if($COLLECTION_LIST.collectionid -contains $c) 
                        $result=$COLLECTION_LIST | ?{$_.collectionId -eq $c} 
                        if($result.membercount -le $MEMBER_COUNT) 
                            $COLLECTION_RESULT +$result 
        #Writing result to HTML page 
        write-host "`n Collection information gathering completed." -foregroundcolor $inputcolor 
        ConvertTo-Html -Head $test -Title $ReportTitle -Body "<h1>SCCM COLLECTIONS HAVING NO DEPLOYMENT OR ADVERTISEMENT</h1>" >  "$strPath" 
        ConvertTo-Html -Head $test -Title $ReportTitle -Body "<h2> Total collections in entire site : $($TOTAL_COUNT_COLLECTION)</h2>" >  "$strPath" 
        ConvertTo-Html -Head $test -Title $ReportTitle -Body "<h2> Total deployments in entire site : $($TOTAL_COUNT_DEPLOYMENT)</h2>" >  "$strPath" 
        $COLLECTION_RESULT | ConvertTo-html  -Head $test -Body "<h2>SCCM COLLECTIONS HAVING NO DEPLOYMENT OR ADVERTISEMENT [Total : $($COLLECTION_COUNT)] </h2>" >> "$strPath" 
        #Launching HTML generated report  
        write-host "`n Opening $strpath report. `n" -foregroundcolor $inputcolor -nonewline  
        Invoke-Item $strPath