⚡ PowerShell 5.1+

Clean Up The Windows Internet cache (INetCache)

Reports the size of the Windows Internet cache (INetCache) and optionally clears it out. Dry-run by default.

By WindowsScripting.com · 1.4 KB
Remove-OldBrowserCache.ps1
<#
.SYNOPSIS
    Cleans up the Windows Internet cache (INetCache).

.DESCRIPTION
    Reports the size of the Windows Internet cache (INetCache) and, with -DeleteFiles, removes its
    contents. Dry-run by default (reports only) so you can see what
    would be freed before anything is deleted.

.PARAMETER DeleteFiles
    Actually delete the contents. Without this switch, the script only reports.

.EXAMPLE
    .\Remove-OldBrowserCache.ps1

.EXAMPLE
    .\Remove-OldBrowserCache.ps1 -DeleteFiles
#>

[CmdletBinding(SupportsShouldProcess)]
param(
    [switch]$DeleteFiles
)

$targetPath = "$env:LOCALAPPDATA\Microsoft\Windows\INetCache"

if (-not (Test-Path $targetPath)) {
    Write-Host "$targetPath does not exist on this machine." -ForegroundColor Yellow
    return
}

$items = Get-ChildItem -Path $targetPath -Force -ErrorAction SilentlyContinue
$totalSize = ($items | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum

Write-Host "$targetPath contains $($items.Count) item(s), $([math]::Round(($totalSize / 1MB), 2)) MB" -ForegroundColor Cyan

if ($DeleteFiles) {
    foreach ($item in $items) {
        if ($PSCmdlet.ShouldProcess($item.FullName, 'Delete')) {
            Remove-Item -Path $item.FullName -Recurse -Force -ErrorAction SilentlyContinue
        }
    }
    Write-Host 'Cleanup complete.' -ForegroundColor Green
} else {
    Write-Host 'Run again with -DeleteFiles to actually remove these items.' -ForegroundColor DarkGray
}