There have been many times where I have needed to retire an old application but I can’t because the application in question is referenced in a few task sequences. Luckily this has become a little easier since ConfigMgr 1906 was released because Microsoft has added the task sequences tab in the application node. Unfortunately you can’t delete the application from the task sequence tab so it is still a tedious task that requires you to open each task sequence and remove or replace the application from each TS. This is why I wrote the following script to automate updating or removing an application from all of the task sequences that references the application.’

Eventually I will create a custom function for this that will make it easier to run but I figured if you are reading this, you are at least somewhat knowledgeable with Powershell :)

How to remove an application:

  1. Add the old application name to the $OldApplicationName variable
  2. Make the $Remove variable equal to $True

How to update an application

  1. Add the old application name to the $OldApplicationName variable
  2. Add the new application name to the $NewApplicationName variable
  3. Make the $Remove variable equal to $False

Code:

# Enter the name of the old application that you want to remove or replace
$OldApplicationName = ""
 
# Enter the new application name that that you want to use to replace the old application
$NewApplicationName = ""
 
# Make the remove variable value $true if you would like to remove an application from all task sequences
$Remove = ""
 
cls
 
$OldApplication = Get-CMApplication "$OldApplicationName"

If($NewApplicationName) {

    $NewApplication = Get-CMApplication "$NewApplicationName"

}
 
$Application = Get-CMApplication -Name "$OldApplicationName"
 
# Get all task sequences that have the old application as a reference
$TaskSequences = Get-CMTaskSequence | Where-Object { $_.References.Package -eq $OldApplication.ModelName -and $_.name -eq "Citrix Build-PDC001FC"  }
 
If($TaskSequences) {
 
    ForEach ($TaskSequence in $TaskSequences) {
 
        Write-Host "Updating $($TaskSequence.Name)"
 
        # Get all install application steps
        $InstallApplicationSteps = (Get-CMTSStepInstallApplication -InputObject (Get-CMTaskSequence -Name $TaskSequence.Name)).Name
 
        ForEach($InstallApplicationStep in $InstallApplicationSteps) {
             
            # Get a list of applications that are in the install application step
            $ApplicationList = (Get-CMTSStepInstallApplication -InputObject $TaskSequence -StepName "$InstallApplicationStep").ApplicationName.Split(",")
 
            # Get application steps that reference the old application
            If($OldApplication.ModelName -in $ApplicationList) {
 
                # Try to replace the old application with the new application
                Try {
 
                    If($Remove -eq $False) {
 
                        $ModelNames = $ApplicationList.Replace($OldApplication.ModelName,$NewApplication.ModelName)
 
                    }
                    Else {
 
                        $ModelNames = $ApplicationList | Where-Object { $_ -ne $OldApplication.ModelName }
 
                    }
 
                }
                Catch {
 
                    Write-Host "Failed to replace or remove old app"
                    Break
 
                }
                If($ModelNames -ne $Null -and $Remove -ne $True) {

                    # Add the new application to the application step
                    Write-Host "- Updating Step: $InstallApplicationStep"
                    Set-CMTSStepInstallApplication -InputObject $TaskSequence -StepName "$InstallApplicationStep" -Application ($ModelNames | ForEach { Get-CMApplication -ModelName $_ })

                }
                Else {

                    Write-Host "- Removing Step: $InstallApplicationStep"
                    Remove-CMTSStepInstallApplication -InputObject $TaskSequence -StepName "$InstallApplicationStep" -Force

                }

 
            }
 
        }
 
    }
 
}
Else {
 
    Write-Host "Could not locate the application in any task sequence!"
 
}