You are probably already aware that SCCM manages its client cache pretty well (delete older objects when free space end). I’ve run into situations where PC has less disk space on the system drive than desired, so I get a compliance settings rule scripts that will help keep the cache a little more tidy.

This script is an example to purge all content in the ccmcache folder that is older than X days. Perfectly work with SCCM 1610. Below the code for compliance rule.

#discover
$MinDays = 20
$UIResourceMgr = New-Object -com “UIResource.UIResourceMgr”
$Cache = $UIResourceMgr.GetCacheInfo()
($Cache.GetCacheElements() |
where-object {[datetime]$_.LastReferenceTime -lt (get-date).adddays(-$mindays)} |
Measure-object).Count

#remediate
$MinDays = 20
$UIResourceMgr = New-Object -ComObject UIResource.UIResourceMgr
$Cache = $UIResourceMgr.GetCacheInfo()
$Cache.GetCacheElements() |
where-object {[datetime]$_.LastReferenceTime -lt (get-date).adddays(-$mindays)} |
foreach {
$Cache.DeleteCacheElement($_.CacheElementID)
}

P. S. Alarm! You may get error in report if you don’t set powershell execution policy “Bypass” in Default Client Settings\Computer Agent.

P. S. S. Script only clear cache that in the client list. If you upgarde or reinstall sccm client on client pc you see that not all folders in c:\windows\ccmcache persist in cache list. In this situation you need another script to clear folder.

Get the list of ccmcache objects run ps script:

$CMObject = New-Object -ComObject “UIResource.UIResourceMgr”
$CMCacheObjects = $CMObject.GetCacheInfo()
$CMCacheObjects.GetCacheElements()

Thanks to Greg’s Systems Management Blog for script.