Cleanup All User Chrome Cache

This script will clean all Chrome cache for all users on a Windows 7 or newer system.

# Define the base directory for user profiles
$usersPath = "C:\Users"
$totalBeforeCleanup = 0
$totalAfterCleanup = 0

# Loop through each user folder in the Users directory
Get-ChildItem -Path $usersPath -Directory | ForEach-Object {
$chromeCachePath = Join-Path -Path $_.FullName -ChildPath "AppData\Local\Google\Chrome\User Data\Default\Cache"
$userBeforeSize = 0
$userAfterSize = 0

# Check if the Chrome Cache folder exists
if (Test-Path -Path $chromeCachePath) {
# Calculate size before cleanup
$userBeforeSize = (Get-ChildItem -Path $chromeCachePath -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum / 1MB
$totalBeforeCleanup += $userBeforeSize

# Remove all files in the Chrome Cache folder
Get-ChildItem -Path $chromeCachePath -Recurse -Force -ErrorAction SilentlyContinue | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

# Calculate size after cleanup (to check if any files were in use and not deleted)
$userAfterSize = (Get-ChildItem -Path $chromeCachePath -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum / 1MB
$totalAfterCleanup += $userAfterSize

# Output results for each user
Write-Output ("User: {0}`nCache size before cleanup: {1:N2} MB`nCache size after cleanup: {2:N2} MB`nFreed up: {3:N2} MB" -f $_.Name, $userBeforeSize, $userAfterSize, ($userBeforeSize - $userAfterSize))
} else {
Write-Output "No Chrome cache folder found for user: $($_.Name)"
}
}

# Output total cleanup summary
$totalCleanedUp = $totalBeforeCleanup - $totalAfterCleanup
Write-Output ("`nTotal Chrome cache size before cleanup: {0:N2} MB" -f $totalBeforeCleanup)
Write-Output ("Total Chrome cache size after cleanup: {0:N2} MB" -f $totalAfterCleanup)
Write-Output ("Total freed up: {0:N2} MB" -f $totalCleanedUp)

Published by

Rich

Just another IT guy.

Leave a Reply

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