SCCM clean client cache script (ccmhache cleanup). You are probably already aware that SCCM manages its client cache pretty well (delete older objects when free space end).

This script is purge all content in the ccmcache folder that is older than X days. Work with any SCCM release (tested with 1706).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()