Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Browser_Cleanup_All.ps1
- #Normal
- #powershell -ExecutionPolicy Bypass -File .\Browser_Cleanup_All.ps1
- #Deeper
- #powershell -ExecutionPolicy Bypass -File .\Browser_Cleanup_All.ps1 -Aggressive
- param(
- [switch]$Aggressive
- )
- Write-Host "========================================"
- Write-Host " Generic Browser Cleanup - Win10 / Win11"
- Write-Host " User: $env:USERNAME"
- Write-Host " Profile: $env:USERPROFILE"
- Write-Host " Aggressive mode: $Aggressive"
- Write-Host "========================================"
- Write-Host ""
- function Get-FolderSizeGB {
- param([string]$Path)
- if (!(Test-Path $Path)) {
- return 0
- }
- $sum = Get-ChildItem $Path -Recurse -Force -ErrorAction SilentlyContinue |
- Where-Object { -not $_.PSIsContainer } |
- Measure-Object Length -Sum
- if ($sum.Sum) {
- return [math]::Round($sum.Sum / 1GB, 2)
- } else {
- return 0
- }
- }
- function Safe-Remove {
- param([string]$Path)
- if (Test-Path $Path) {
- try {
- Write-Host "Deleting: $Path"
- Remove-Item $Path -Recurse -Force -ErrorAction SilentlyContinue
- } catch {
- Write-Host "Could not delete: $Path"
- }
- }
- }
- function Backup-File {
- param(
- [string]$SourceFile,
- [string]$DestinationFolder
- )
- if (Test-Path $SourceFile) {
- New-Item -ItemType Directory -Path $DestinationFolder -Force | Out-Null
- Copy-Item $SourceFile $DestinationFolder -Force -ErrorAction SilentlyContinue
- }
- }
- function Backup-Chromium-Browser {
- param(
- [string]$BrowserName,
- [string]$UserDataPath,
- [string]$BackupRoot
- )
- if (!(Test-Path $UserDataPath)) {
- return
- }
- $browserBackup = Join-Path $BackupRoot $BrowserName
- New-Item -ItemType Directory -Path $browserBackup -Force | Out-Null
- Backup-File -SourceFile (Join-Path $UserDataPath "Local State") -DestinationFolder $browserBackup
- $profiles = Get-ChildItem $UserDataPath -Directory -Force -ErrorAction SilentlyContinue |
- Where-Object {
- $_.Name -eq "Default" -or
- $_.Name -like "Profile *" -or
- $_.Name -eq "Guest Profile" -or
- $_.Name -eq "System Profile"
- }
- foreach ($profile in $profiles) {
- $profileBackup = Join-Path $browserBackup $profile.Name
- New-Item -ItemType Directory -Path $profileBackup -Force | Out-Null
- $importantFiles = @(
- "Bookmarks",
- "Bookmarks.bak",
- "Login Data",
- "Login Data For Account",
- "Preferences",
- "Secure Preferences",
- "History",
- "Cookies",
- "Web Data",
- "Favicons",
- "Shortcuts",
- "Top Sites"
- )
- foreach ($file in $importantFiles) {
- Backup-File -SourceFile (Join-Path $profile.FullName $file) -DestinationFolder $profileBackup
- }
- }
- }
- function Clean-Chromium-Browser {
- param(
- [string]$BrowserName,
- [string]$RootPath,
- [string]$UserDataPath,
- [string]$ProcessName,
- [string]$BackupRoot
- )
- if (!(Test-Path $RootPath)) {
- Write-Host "$BrowserName not found. Skipping."
- return
- }
- Write-Host ""
- Write-Host "----------------------------------------"
- Write-Host "Cleaning $BrowserName"
- Write-Host "Path: $RootPath"
- Write-Host "----------------------------------------"
- $before = Get-FolderSizeGB -Path $RootPath
- Write-Host "$BrowserName size before: $before GB"
- taskkill /IM $ProcessName /F 2>$null | Out-Null
- Backup-Chromium-Browser -BrowserName $BrowserName -UserDataPath $UserDataPath -BackupRoot $BackupRoot
- $safeTargets = @(
- "Cache",
- "Code Cache",
- "GPUCache",
- "DawnCache",
- "ShaderCache",
- "GrShaderCache",
- "GraphiteDawnCache",
- "Media Cache",
- "Crashpad\reports",
- "Crashpad\attachments",
- "BrowserMetrics",
- "optimization_guide_prediction_model_downloads",
- "CertificateRevocation",
- "Safe Browsing",
- "Application Cache"
- )
- $aggressiveTargets = @(
- "File System",
- "blob_storage",
- "Service Worker\CacheStorage",
- "Service Worker\ScriptCache",
- "IndexedDB\https_*",
- "Session Storage",
- "Local Storage\leveldb"
- )
- $targets = $safeTargets
- if ($Aggressive) {
- $targets += $aggressiveTargets
- }
- $locations = @()
- if (Test-Path $UserDataPath) {
- $locations += $UserDataPath
- $locations += Get-ChildItem $UserDataPath -Directory -Force -ErrorAction SilentlyContinue |
- Where-Object {
- $_.Name -eq "Default" -or
- $_.Name -like "Profile *" -or
- $_.Name -eq "Guest Profile" -or
- $_.Name -eq "System Profile"
- } |
- Select-Object -ExpandProperty FullName
- }
- foreach ($loc in $locations) {
- foreach ($target in $targets) {
- Safe-Remove -Path (Join-Path $loc $target)
- }
- }
- $updatePath = Join-Path $RootPath "Update"
- if (Test-Path $updatePath) {
- Get-ChildItem $updatePath -Recurse -Force -ErrorAction SilentlyContinue |
- Where-Object {
- $_.Name -like "*.tmp" -or
- $_.Name -like "*.log" -or
- $_.Name -like "*.crdownload" -or
- $_.FullName -match "\\Download\\|\\Install\\|\\Temp\\"
- } |
- ForEach-Object {
- Safe-Remove -Path $_.FullName
- }
- }
- $after = Get-FolderSizeGB -Path $RootPath
- Write-Host "$BrowserName size after : $after GB"
- Write-Host "$BrowserName reduced by : $([math]::Round($before - $after, 2)) GB"
- }
- function Backup-Firefox {
- param(
- [string]$FirefoxPath,
- [string]$BackupRoot
- )
- $profilesRoot = Join-Path $FirefoxPath "Profiles"
- if (!(Test-Path $profilesRoot)) {
- return
- }
- $firefoxBackup = Join-Path $BackupRoot "Firefox"
- New-Item -ItemType Directory -Path $firefoxBackup -Force | Out-Null
- Backup-File -SourceFile (Join-Path $FirefoxPath "profiles.ini") -DestinationFolder $firefoxBackup
- Backup-File -SourceFile (Join-Path $FirefoxPath "installs.ini") -DestinationFolder $firefoxBackup
- $profiles = Get-ChildItem $profilesRoot -Directory -Force -ErrorAction SilentlyContinue
- foreach ($profile in $profiles) {
- $profileBackup = Join-Path $firefoxBackup $profile.Name
- New-Item -ItemType Directory -Path $profileBackup -Force | Out-Null
- $importantFiles = @(
- "places.sqlite",
- "favicons.sqlite",
- "key4.db",
- "logins.json",
- "cookies.sqlite",
- "permissions.sqlite",
- "prefs.js",
- "formhistory.sqlite",
- "cert9.db",
- "handlers.json",
- "extensions.json"
- )
- foreach ($file in $importantFiles) {
- Backup-File -SourceFile (Join-Path $profile.FullName $file) -DestinationFolder $profileBackup
- }
- }
- }
- function Clean-Firefox {
- param(
- [string]$FirefoxPath,
- [string]$BackupRoot
- )
- if (!(Test-Path $FirefoxPath)) {
- Write-Host "Firefox not found. Skipping."
- return
- }
- Write-Host ""
- Write-Host "----------------------------------------"
- Write-Host "Cleaning Firefox"
- Write-Host "Path: $FirefoxPath"
- Write-Host "----------------------------------------"
- $before = Get-FolderSizeGB -Path $FirefoxPath
- Write-Host "Firefox size before: $before GB"
- taskkill /IM firefox.exe /F 2>$null | Out-Null
- Backup-Firefox -FirefoxPath $FirefoxPath -BackupRoot $BackupRoot
- $profilesRoot = Join-Path $FirefoxPath "Profiles"
- if (Test-Path $profilesRoot) {
- $profiles = Get-ChildItem $profilesRoot -Directory -Force -ErrorAction SilentlyContinue
- foreach ($profile in $profiles) {
- $safeTargets = @(
- "cache2",
- "startupCache",
- "shader-cache",
- "jumpListCache",
- "thumbnails",
- "minidumps",
- "datareporting",
- "crashes",
- "saved-telemetry-pings"
- )
- $aggressiveTargets = @(
- "storage\default\https+++*",
- "storage\temporary",
- "storage\ls-archive.sqlite",
- "sessionstore-backups"
- )
- $targets = $safeTargets
- if ($Aggressive) {
- $targets += $aggressiveTargets
- }
- foreach ($target in $targets) {
- Safe-Remove -Path (Join-Path $profile.FullName $target)
- }
- }
- }
- $after = Get-FolderSizeGB -Path $FirefoxPath
- Write-Host "Firefox size after : $after GB"
- Write-Host "Firefox reduced by : $([math]::Round($before - $after, 2)) GB"
- }
- $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
- $backupRoot = Join-Path $env:USERPROFILE "Desktop\Browser_Backup_Important_$timestamp"
- New-Item -ItemType Directory -Path $backupRoot -Force | Out-Null
- Write-Host "Backup folder:"
- Write-Host $backupRoot
- Write-Host ""
- $browserList = @(
- @{
- Name = "Chrome"
- Root = Join-Path $env:LOCALAPPDATA "Google"
- UserData = Join-Path $env:LOCALAPPDATA "Google\Chrome\User Data"
- Process = "chrome.exe"
- },
- @{
- Name = "Edge"
- Root = Join-Path $env:LOCALAPPDATA "Microsoft\Edge"
- UserData = Join-Path $env:LOCALAPPDATA "Microsoft\Edge\User Data"
- Process = "msedge.exe"
- },
- @{
- Name = "Brave"
- Root = Join-Path $env:LOCALAPPDATA "BraveSoftware"
- UserData = Join-Path $env:LOCALAPPDATA "BraveSoftware\Brave-Browser\User Data"
- Process = "brave.exe"
- }
- )
- $totalBefore = 0
- $totalAfter = 0
- foreach ($browser in $browserList) {
- if (Test-Path $browser.Root) {
- $totalBefore += Get-FolderSizeGB -Path $browser.Root
- }
- }
- $firefoxPath = Join-Path $env:APPDATA "Mozilla\Firefox"
- if (Test-Path $firefoxPath) {
- $totalBefore += Get-FolderSizeGB -Path $firefoxPath
- }
- foreach ($browser in $browserList) {
- Clean-Chromium-Browser `
- -BrowserName $browser.Name `
- -RootPath $browser.Root `
- -UserDataPath $browser.UserData `
- -ProcessName $browser.Process `
- -BackupRoot $backupRoot
- }
- Clean-Firefox -FirefoxPath $firefoxPath -BackupRoot $backupRoot
- foreach ($browser in $browserList) {
- if (Test-Path $browser.Root) {
- $totalAfter += Get-FolderSizeGB -Path $browser.Root
- }
- }
- if (Test-Path $firefoxPath) {
- $totalAfter += Get-FolderSizeGB -Path $firefoxPath
- }
- Write-Host ""
- Write-Host "========================================"
- Write-Host " Cleanup Completed"
- Write-Host "========================================"
- Write-Host "Total browser size before: $([math]::Round($totalBefore, 2)) GB"
- Write-Host "Total browser size after : $([math]::Round($totalAfter, 2)) GB"
- Write-Host "Total space reduced : $([math]::Round($totalBefore - $totalAfter, 2)) GB"
- Write-Host ""
- Write-Host "Backup created at:"
- Write-Host $backupRoot
- Write-Host ""
- Write-Host "Passwords, bookmarks, settings, extensions, history, and cookies were not intentionally deleted."
- Write-Host ""
- Write-Host "Note:"
- Write-Host "Chromium passwords are tied to Windows DPAPI and Local State."
- Write-Host "Firefox passwords are backed up using logins.json and key4.db."
- Write-Host "Use backup only on the same Windows user profile/machine unless you know what you are doing."
- Write-Host "========================================"
Advertisement
Add Comment
Please, Sign In to add comment