Clears all SCCM client cache content that hasn't been referenced in 30 days including persistent cache items.
.NOTES
Created by: Jason Wasser @wasserja
Modified: 7/11/2017 11:43:18 AM
#>functionClear-CMClientCache{[CmdletBinding(SupportsShouldProcess=$true)]param([string[]]$ComputerName=$env:COMPUTERNAME,[int]$CacheAgeinDays=0,[switch]$Force)begin{functionClean-ClientCache{param([int]$CacheAgeinDays,[bool]$WhatIf,[bool]$Force)$VerbosePreference='Continue'Write-Verbose"Beginning clearing of SCCM Cache older than $CacheAgeinDays days"$UIResourceMgr=New-Object-ComObjectUIResource.UIResourceMgr$Cache=$UIResourceMgr.GetCacheInfo()$CacheElements=$Cache.GetCacheElements()|Where-Object-FilterScript{$_.LastReferenceTime-lt(Get-Date).AddDays(-$CacheAgeInDays)}if($CacheElements){if($CacheElements.Count){Write-Verbose-Message"Found $($CacheElements.Count) cache elements on $($env:COMPUTERNAME) continuing to clear the elements in the cache."}else{Write-Verbose-Message"Found a cache element on $($env:COMPUTERNAME) continuing to clear the elements in the cache."}foreach($Elementin$CacheElements){if($WhatIf){if($Force){Write-Verbose"What if: Forcing deletion of CacheElement with PackageID $($Element.ContentID) from $($Element.Location)"}else{Write-Verbose"What if: Deleting CacheElement with PackageID $($Element.ContentID) from $($Element.Location)"}}else{if($Force){Write-Verbose"Forcing deletion of CacheElement with PackageID $($Element.ContentID) from $($Element.Location)"$Cache.DeleteCacheElementEx($Element.CacheElementID,$true)}else{Write-Verbose"Deleting CacheElement with PackageID $($Element.ContentID) from $($Element.Location)"$Cache.DeleteCacheElement($Element.CacheElementID)}}}}else{Write-Verbose-Message"No cache elements found on $($env:COMPUTERNAME)"}}}process{