![Automatically update your task sequence](https://www.joseespitia.com/wp-content/uploads/2020/05/tasksequence.png)
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:
- Add the old application name to the $OldApplicationName variable
- Make the $Remove variable equal to $True
How to update an application
- Add the old application name to the $OldApplicationName variable
- Add the new application name to the $NewApplicationName variable
- 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" $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 } 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 } # 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 "Could not locate the application in any task sequence!" }
Giz
Thank you for the help!
Huseyin Caglar
Hi 🙂
Thanks a lot, I was just looking for something like this.
Regards
Jose Espitia
I’m glad that I could help out Huseyin!
Roberto
Some steps in that script are a little bit strange, some don’t work with 2107. I needed to make a few changes and now it works great. Thanks for your work, it saved me time.
Jose Espitia
Glad it worked for you Roberto! I haven’t tested it on 2107 yet but feel free to share the solution for others!
Collin Corrion
It looks like the packages references aren’t loaded with -Lazy any longer. I removed those from the script, ie:
$TaskSequences = Get-CMTaskSequence | Where-Object { $_.References.Package -eq $OldApplication.ModelName }
and it works.
Jose Espitia
Thanks for the heads up Collin!
Daniel
Hello Jose 🙂
Great script.
Something I noticed is that if the step name exists multiple times within one tasks sequence (I guess this is bad design of the task sequence) it can cause issues as it will update multiple steps at the same time.
I fixed it by adding a check to see if the step name exists multiple times and if so just do nothing.
Regards
Jose Espitia
Thanks for the info Daniel! Do you mind sharing the solution for others? 🙂
Ariel Funches
Just found this. Thank you!
I was struggling to get this to work initially where it would remove the application instead of updating it. It wasn’t until I set Remove = “$false” that it started to work correctly.