PowerShell Script to Automate Running ContentLibraryCleanup.exe Against All DPs in SCCM Site

This is a rough script that automates the running of the content library cleanup tool available after SCCM Build 1702. The script will connect to a SCCM site to build a list of distribution points to run against. This needs to be ran on the primary site server under account with full admin rights in SCCM. It checks four possible locations for the PS module and the cleanup tool. The script prompts for the SCCM site code, the FQDN of the primary site server, and if you want the tool to delete orphaned content or just log to the tools default log location.

More information on the cool can be found at https://docs.microsoft.com/en-us/sccm/core/plan-design/hierarchy/content-library-cleanup-tool

<# .SYNOPSIS This script automates the running of the content library cleanup tool available after SCCM Build 1702. .DESCRIPTION The script will connect to SCCM to build a list of DPs to run against. This needs to be ran on the primary site server under account with full admin rights in SCCM. It checks four possible locations for the PS module and the cleanup tool .PARAMETER Site SCCM Site Code .PARAMETER Primary FQDN of the SCCM Primary Site server .PARAMETER Delete Sets if tool just logs or deletes content. Delete requires yes or y entered. Other entries will force to log only .EXAMPLE ContentLibraryCleanup.ps1 -Site CM1 -Primary sccm.organization.com -Delete y #>

param(
[Parameter(ParameterSetName=1,Mandatory=$true)]
[string]$Site,
[Parameter(ParameterSetName=1,Mandatory=$true)]
[string]$Primary,
[Parameter(ParameterSetName=1,Mandatory=$true)]
[string]$Delete
)

#Get SCCM Console Install Path to locate PS Module.
$pspath = $Env:SMS_ADMIN_UI_PATH.TrimEnd("i386") + "ConfigurationManager.psd1"

if (Test-Path "$pspath")
{
Import-Module "$pspath"
}
else
{
$a = New-Object -ComObject WScript.Shell
$b = $a.popup("Could Not Load SCCM PowerShell Module.", 10, "Error", 0)
exit
}

#Set ContentLibraryCleanup.exe path
if (Test-Path "C:\Program Files\Microsoft Configuration Manager\cd.latest\SMSSETUP\TOOLS\ContentLibraryCleanup\ContentLibraryCleanup.exe")
{
$tooldrive = 'c:'
$toolpath = "Program Files\Microsoft Configuration Manager\cd.latest\SMSSETUP\TOOLS\ContentLibraryCleanup"
}
elseif (Test-Path "C:\Program Files (x86)\Microsoft Configuration Manager\cd.latest\SMSSETUP\TOOLS\ContentLibraryCleanup\ContentLibraryCleanup.exe")
{
$tooldrive = 'c:'
$toolpath = "Program Files (x86)\Microsoft Configuration Manager\cd.latest\SMSSETUP\TOOLS\ContentLibraryCleanup"
}
elseif (Test-Path "d:\Program Files\Microsoft Configuration Manager\cd.latest\SMSSETUP\TOOLS\ContentLibraryCleanup\ContentLibraryCleanup.exe")
{
$tooldrive = 'd:'
$toolpath = "Program Files\Microsoft Configuration Manager\cd.latest\SMSSETUP\TOOLS\ContentLibraryCleanup"
}
elseif (Test-Path "d:\Program Files (x86)\Microsoft Configuration Manager\cd.latest\SMSSETUP\TOOLS\ContentLibraryCleanup\ContentLibraryCleanup.exe")
{
$tooldrive = 'd:'
$toolpath = "Program Files (x86)\Microsoft Configuration Manager\cd.latest\SMSSETUP\TOOLS\ContentLibraryCleanup"
}
else
{
$a = New-Object -ComObject WScript.Shell
$b = $a.popup("Could Not Find ContentLibraryCleanup.exe.", 10, "Error", 0)
exit
}

#sets delete of content or not
if ($Delete -eq 'yes' -or $Delete -eq 'y')
{
$del = '/delete /q'
}
else
{
$del = ''
}

#set directory to site server
$sitecode = "$site" + ":"
Set-Location $sitecode

#gets list of DPs
$dplist = Get-CMDistributionPoint | Select-Object -ExpandProperty NetworkOSPath

#changes directory to location of tool
cd $tooldrive
cd\
cd $toolpath

#loops through DP list and runs tool against list
Foreach ($item in $dplist)
{
$item = $item.Trim("")
$arg = ".\ContentLibraryCleanup.exe /dp $item /ps $Primary /sc $site $del"
Invoke-Expression $arg
}