Configuration Manager Client Cache Cleanup Script

2 Min Read

Configuration Manager Client Cache Cleanup Script – Free up Disk Space Easily. You are probably already aware that SCCM manages its client cache pretty well (deletes older objects when free space ends).

This script purges all content in the ccmcache folder that is older than X days. Work with any SCCM release. Below is the code for the 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)
}

NOTE: You may get an error in the report if you don’t set the PowerShell execution policy “Bypass” in Default Client Settings\Computer Agent in SCCM Console.

NOTE: Script only clears cache in the client list. If you upgrade or reinstall SCCM client on the client pc you see that not all folders in c:\windows\ccmcache persist in the cache list. In this situation, you need another script to clear the folder.

To get the list of ccmcache objects run the ps script:

$CMObject = New-Object -ComObject UIResource.UIResourceMgr
$CMCacheObjects = $CMObject.GetCacheInfo()
$CMCacheObjects.GetCacheElements()
2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version