# Script to clear Chrome and Edge browser caches for all user profiles
# Get all user profile directories, excluding system accounts
$users = Get-ChildItem 'C:\Users' -Directory | Where-Object { $_.Name -notin @('Public', 'Default', 'Default User', 'All Users') }
# Track statistics for reporting
$totalClearedSize = 0
$processedUsers = 0
# Force close all browser instances
Write-Host "Closing browser processes..." -ForegroundColor Yellow
$browserProcesses = @("chrome", "msedge")
foreach ($process in $browserProcesses) {
if (Get-Process -Name $process -ErrorAction SilentlyContinue) {
Stop-Process -Name $process -Force -ErrorAction SilentlyContinue
Write-Host " Closed all $process processes" -ForegroundColor Yellow
# Give browsers time to fully close
Start-Sleep -Seconds 2
}
}
foreach ($user in $users) {
$userName = $user.Name
$processedUsers++
Write-Host "Processing user: $userName..." -ForegroundColor Cyan
# Define browser paths in a structured way for better maintainability
$browserPaths = @{
Chrome = @{
MainCache = Join-Path $user.FullName 'AppData\Local\Google\Chrome\User Data\Default\Cache'
SWCache = Join-Path $user.FullName 'AppData\Local\Google\Chrome\User Data\Default\Service Worker\CacheStorage'
Network = Join-Path $user.FullName 'AppData\Local\Google\Chrome\User Data\Default\Network'
GPUCache = Join-Path $user.FullName 'AppData\Local\Google\Chrome\User Data\Default\GPUCache'
}
Edge = @{
MainCache = Join-Path $user.FullName 'AppData\Local\Microsoft\Edge\User Data\Default\Cache'
SWCache = Join-Path $user.FullName 'AppData\Local\Microsoft\Edge\User Data\Default\Service Worker\CacheStorage'
Network = Join-Path $user.FullName 'AppData\Local\Microsoft\Edge\User Data\Default\Network'
GPUCache = Join-Path $user.FullName 'AppData\Local\Microsoft\Edge\User Data\Default\GPUCache'
}
}
# Process each browser's cache locations
foreach ($browser in $browserPaths.Keys) {
$cacheLocations = $browserPaths[$browser]
foreach ($cacheType in $cacheLocations.Keys) {
$path = $cacheLocations[$cacheType]
if (Test-Path $path) {
# Get size before cleanup for reporting
$sizeBefore = (Get-ChildItem $path -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
if ($null -eq $sizeBefore) { $sizeBefore = 0 }
# Clear the cache
Remove-Item -Path "$path\*" -Recurse -Force -ErrorAction SilentlyContinue
# Calculate freed space
$clearedSize = $sizeBefore / 1MB
$totalClearedSize += $clearedSize
Write-Host " Cleared $browser $cacheType cache: $([math]::Round($clearedSize, 2)) MB" -ForegroundColor Green
}
}
}
Write-Host "Completed processing user: $userName" -ForegroundColor Cyan
Write-Host "-----------------------------------------"
}
# Summary report
Write-Host "Cache Cleanup Summary:" -ForegroundColor Magenta
Write-Host "Users processed: $processedUsers" -ForegroundColor Magenta
Write-Host "Total space cleared: $([math]::Round($totalClearedSize, 2)) MB" -ForegroundColor Magenta