Skip to content

Instantly share code, notes, and snippets.

@TScalzott
Created August 1, 2019 14:52
Show Gist options
  • Save TScalzott/670473e5213aaa91f45934335a8c6dd1 to your computer and use it in GitHub Desktop.
Save TScalzott/670473e5213aaa91f45934335a8c6dd1 to your computer and use it in GitHub Desktop.
Clean IE and Chrome cache for all stale users. Handy on RDS servers.
<#
.SYNOPSIS
Clean old cached items.
.DESCRIPTION
Forcibly clear Google Chrome and IE Cache for all users staler than a certain number of days
.PARAMETER Days
The number of days stale. Determined by LastUsed time from the user's profile.
#>
[cmdletbinding(SupportsShouldProcess = $True)]
param(
[Parameter(Mandatory = $false)][int]$numDays = 7
)
#$numDays = 7
$staleDate = (Get-Date).AddDays( - ($numDays))
$LogFile = "\LogFiles\Clean-Cache\Clean-Cache-$((Get-Date -format 'yyyyMMdd')).log"
if ([bool]$WhatIfPreference.IsPresent) {
Write-Output "**Running in WHATIF mode**"
}
Write-Output "Searching for profiles older than $($numDays) days ago ($($staleDate))..." |
Tee-Object -FilePath $LogFile -Append
$Stale = Get-WmiObject -Class Win32_UserProfile -Filter "Special = false" |
Where-Object { $_.LocalPath -notmatch "\\(Admin|Owner)" -and ($_.ConvertToDateTime($_.LastUseTime) -lt $staleDate) } |
Sort-Object LocalPath
foreach ($p in $Stale) {
Write-Output "Cleaning $($p.LocalPath) Google Chrome cache..." |
Tee-Object -FilePath $LogFile -Append
Remove-Item -path "$($p.LocalPath)\AppData\Local\Google\Chrome\User Data\Default\Cache\*" -Recurse -Force `
-ErrorAction SilentlyContinue -WhatIf:([bool]$WhatIfPreference.IsPresent) -Verbose |
Tee-Object -FilePath $LogFile -Append
Write-Output "Cleaning $($p.LocalPath) IE cache..." |
Tee-Object -FilePath $LogFile -Append
Remove-Item -path "$($p.LocalPath)\AppData\Local\Microsoft\Windows\InetCache\*" -Recurse -Force `
-ErrorAction SilentlyContinue -WhatIf:([bool]$WhatIfPreference.IsPresent) |
Tee-Object -FilePath $LogFile -Append
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment