Please follow the below PowerShell script to check SCCM device collections with No deployment and delete it if the collection is no longer required.
We need to modify the report path in the 1st line of this script, highlighted in yellow
$reportpath = "K:OSDLogsResult.csv" # Provide CSV path with CSV Name Import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + 'ConfigurationManager.psd1') $SiteCode = Get-PSDrive -PSProvider CMSITE Set-location $SiteCode":" $details = @() $CollectionList = Get-CmCollection | Where-Object {$_.CollectionID -notlike 'SMS*' -and $_.CollectionType -eq '2'} | Select-Object -Property Name,MemberCount,CollectionID,IsReferenceCollection foreach ($Collection in $CollectionList) { $NumCollectionMembers = $Collection.MemberCount $CollectionID = $Collection.CollectionID $GetDeployment = Get-CMDeployment | Where-Object {$_.CollectionID -eq $Collection.CollectionID} if ($GetDeployment -eq $null) { $collectionName = $Collection.Name $Membercount = $Collection.MemberCount $Result = [ordered]@{ Collection_NAME = "$collectionName" Member_Count = "$Membercount" } $Details += New-Object PSObject -Property $Result } }
The output of this script will be as shown below –
As per the above script, we are not deleting the collection which doesn’t have any deployments targetted to it. If we wanted to delete the collections with no deployments targetted, we need to modify the script under “IF” loop as shown below.
Please note: The collection will be deleted forcefully without any notifications as per the below script, you can make the modification if required
Remove-CMCollection -Id $CollectionID -Force $collectionName = $Collection.Name $Membercount = $Collection.MemberCount $Result = [ordered] @{ Collection_NAME = "$collectionName" MemberCount = "$Membercount" } $Details += New-Object PSObject -Property $Result