Remove all ConfigMgr Applications in a Folder

Updated:  8/25/18 to correct issue in script

Recently I was cleaning up the Application objects in SCCM. All applications used in a task sequence were in a folder called “OSD – Workstation”. I wanted to delete any applications in this folder that were not being directly referenced in an existing task sequence and were not part of a deployment. To accomplish this I used the following PowerShell script.

$SiteServer='CM01'
$SiteCode = 'CAS'
$FolderName = '/OSD - Workstation'

#region Connect To SCCM
$IsSCCMLoaded = Get-Module |Where {$_.Name -eq 'ConfigurationManager'}
If ($IsSCCMLoaded -eq $null)
    {import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1')}
$SiteDriveLetter="$SiteCode`:"
If(!(Test-Path "$SiteDriveLetter"))
{new-psdrive -Name "$SiteCode" -PSProvider CMSite -Root "$SiteServer"}

#endregion connect to sccm

 
$Applications=Get-WmiObject -ComputerName $SiteServer -Namespace root\SMS\site_$SiteCode -Class SMS_Applicationlatest|Select LocalizedDisplayName, CI_ID, CI_UniqueID, CreatedBy, ObjectPath, IsDeployed, NumberOfDependentTS, NumberOfDependentDTs

#Create Array of Applicaitons in a folder, that are not referenced in a task sequence and are not listed as a dependent on another app
$AppsToDelete = $Applications |Where {$_.ObjectPath -eq $FolderName -and $_.NumberOfDependentTS -eq 0 -and $_.IsDeployed -eq $false}
set-location $SiteDriveLetter
forEach($app in $AppsToDelete)
{
    Remove-CMApplication -Id $app.cI_ID -force
}

This script connects to the WMI on the site server to obtain the folder path for each application. It then filters the results on only applications in the specified folder name, with No dependent task sequences, no dependent deployment types and no deployments.

When running for the first time, I recommend changing the “Remove-CMApplication -Id $app.cI_ID -Force” line to “Remove-CMApplication -Id $app.cI_ID -WhatIf” this will allow you to review what will be deleted.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s