Creating Pre-Caching Task Sequences using PowerShell

Here is a PowerShell script that will create a pre-caching task sequence for ConfigMgr. In this example I'm enumerating all driver packages, and creating a pre-caching task sequence with those packages.

If you want to learn more about the core concept of pre-caching for P2P in ConfigMgr, check this post: Pre-Caching Content for P2P in a ConfigMgr Environment

The script will create a single root group, that has a condition on it to never run. This way you can safely configure your deployment of the pre-caching sequence to always download content locally before starting the task sequence. By setting that deployment option, you force ConfigMgr to use BITS to download all content in the ConfigMgr Client Cache (and the BranchCache cache if using that).

The Script

Here is the script that creates the task sequence.

Note: I purposely created to create an action for each package, so that you easily can remove a single package if needed, plus to avoid having to add a loop in the script when adding more than 32 packages (a single download package content step only supports 32 packages). I was lazy, OK 🙂

$SiteServer = ""
$SiteCode = "PS1"
$TaskSequenceName = "Pre-Cache Driver Packages"

# Connect to ConfigMgr
$Namespace = "root\SMS\Site_" + $SiteCode
Import-Module (Join-Path $(Split-Path $ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1) -Verbose:$false
Set-Location "$SiteCode`:"

# Create a new Task Sequence
$TS = New-CMTaskSequence -CustomTaskSequence -Name $TaskSequenceName

# Create Root Group with a condition to never run
$GroupCondition = New-CMTaskSequenceStepConditionVariable -OperatorType Equals -ConditionVariableName "NeverTrue" -ConditionVariableValue "True"
$Group = New-CMTaskSequenceGroup -Name "Pre-Cache Package Content" -Condition $GroupCondition
Add-CMTaskSequenceStep -InsertStepStartIndex 0 -TaskSequenceName $TS.Name -Step $Group 

# Create a list of driver packages to add
$Packages = Get-CMPackage -Name "Drivers*" -Fast

# Create a Download Package Content action per package
foreach ($Package in $Packages){
    # Make sure the Download Package Content action name is no longer than 50 characters
    $DCPActionName = $Package.Name
    If ($DCPActionName.Length -gt 50){ 
        $DCPActionName = ($Package.Name).Substring(0,50)

    # Create each DownloadPackageContent action
    $PackageContentDrivers = New-CMTSStepDownloadPackageContent -Name $DCPActionName -AddPackage $Package
    Set-CMTaskSequenceGroup -TaskSequenceName $TS.Name -AddStep $PackageContentDrivers -InsertStepStartIndex 0 
Group with condition to never run
Driver packages added
About the author

Johan Arwidmark

5 1 vote
Article Rating
Notify of
Inline Feedbacks
View all comments