mayankjoin3

Browser_Cleanup_All

Jun 16th, 2026
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Browser_Cleanup_All.ps1
  2. #Normal
  3. #powershell -ExecutionPolicy Bypass -File .\Browser_Cleanup_All.ps1
  4.  
  5. #Deeper
  6. #powershell -ExecutionPolicy Bypass -File .\Browser_Cleanup_All.ps1 -Aggressive
  7.  
  8.  
  9. param(
  10.     [switch]$Aggressive
  11. )
  12.  
  13. Write-Host "========================================"
  14. Write-Host " Generic Browser Cleanup - Win10 / Win11"
  15. Write-Host " User: $env:USERNAME"
  16. Write-Host " Profile: $env:USERPROFILE"
  17. Write-Host " Aggressive mode: $Aggressive"
  18. Write-Host "========================================"
  19. Write-Host ""
  20.  
  21. function Get-FolderSizeGB {
  22.     param([string]$Path)
  23.  
  24.     if (!(Test-Path $Path)) {
  25.         return 0
  26.     }
  27.  
  28.     $sum = Get-ChildItem $Path -Recurse -Force -ErrorAction SilentlyContinue |
  29.         Where-Object { -not $_.PSIsContainer } |
  30.         Measure-Object Length -Sum
  31.  
  32.     if ($sum.Sum) {
  33.         return [math]::Round($sum.Sum / 1GB, 2)
  34.     } else {
  35.         return 0
  36.     }
  37. }
  38.  
  39. function Safe-Remove {
  40.     param([string]$Path)
  41.  
  42.     if (Test-Path $Path) {
  43.         try {
  44.             Write-Host "Deleting: $Path"
  45.             Remove-Item $Path -Recurse -Force -ErrorAction SilentlyContinue
  46.         } catch {
  47.             Write-Host "Could not delete: $Path"
  48.         }
  49.     }
  50. }
  51.  
  52. function Backup-File {
  53.     param(
  54.         [string]$SourceFile,
  55.         [string]$DestinationFolder
  56.     )
  57.  
  58.     if (Test-Path $SourceFile) {
  59.         New-Item -ItemType Directory -Path $DestinationFolder -Force | Out-Null
  60.         Copy-Item $SourceFile $DestinationFolder -Force -ErrorAction SilentlyContinue
  61.     }
  62. }
  63.  
  64. function Backup-Chromium-Browser {
  65.     param(
  66.         [string]$BrowserName,
  67.         [string]$UserDataPath,
  68.         [string]$BackupRoot
  69.     )
  70.  
  71.     if (!(Test-Path $UserDataPath)) {
  72.         return
  73.     }
  74.  
  75.     $browserBackup = Join-Path $BackupRoot $BrowserName
  76.     New-Item -ItemType Directory -Path $browserBackup -Force | Out-Null
  77.  
  78.     Backup-File -SourceFile (Join-Path $UserDataPath "Local State") -DestinationFolder $browserBackup
  79.  
  80.     $profiles = Get-ChildItem $UserDataPath -Directory -Force -ErrorAction SilentlyContinue |
  81.         Where-Object {
  82.             $_.Name -eq "Default" -or
  83.             $_.Name -like "Profile *" -or
  84.             $_.Name -eq "Guest Profile" -or
  85.             $_.Name -eq "System Profile"
  86.         }
  87.  
  88.     foreach ($profile in $profiles) {
  89.         $profileBackup = Join-Path $browserBackup $profile.Name
  90.         New-Item -ItemType Directory -Path $profileBackup -Force | Out-Null
  91.  
  92.         $importantFiles = @(
  93.             "Bookmarks",
  94.             "Bookmarks.bak",
  95.             "Login Data",
  96.             "Login Data For Account",
  97.             "Preferences",
  98.             "Secure Preferences",
  99.             "History",
  100.             "Cookies",
  101.             "Web Data",
  102.             "Favicons",
  103.             "Shortcuts",
  104.             "Top Sites"
  105.         )
  106.  
  107.         foreach ($file in $importantFiles) {
  108.             Backup-File -SourceFile (Join-Path $profile.FullName $file) -DestinationFolder $profileBackup
  109.         }
  110.     }
  111. }
  112.  
  113. function Clean-Chromium-Browser {
  114.     param(
  115.         [string]$BrowserName,
  116.         [string]$RootPath,
  117.         [string]$UserDataPath,
  118.         [string]$ProcessName,
  119.         [string]$BackupRoot
  120.     )
  121.  
  122.     if (!(Test-Path $RootPath)) {
  123.         Write-Host "$BrowserName not found. Skipping."
  124.         return
  125.     }
  126.  
  127.     Write-Host ""
  128.     Write-Host "----------------------------------------"
  129.     Write-Host "Cleaning $BrowserName"
  130.     Write-Host "Path: $RootPath"
  131.     Write-Host "----------------------------------------"
  132.  
  133.     $before = Get-FolderSizeGB -Path $RootPath
  134.     Write-Host "$BrowserName size before: $before GB"
  135.  
  136.     taskkill /IM $ProcessName /F 2>$null | Out-Null
  137.  
  138.     Backup-Chromium-Browser -BrowserName $BrowserName -UserDataPath $UserDataPath -BackupRoot $BackupRoot
  139.  
  140.     $safeTargets = @(
  141.         "Cache",
  142.         "Code Cache",
  143.         "GPUCache",
  144.         "DawnCache",
  145.         "ShaderCache",
  146.         "GrShaderCache",
  147.         "GraphiteDawnCache",
  148.         "Media Cache",
  149.         "Crashpad\reports",
  150.         "Crashpad\attachments",
  151.         "BrowserMetrics",
  152.         "optimization_guide_prediction_model_downloads",
  153.         "CertificateRevocation",
  154.         "Safe Browsing",
  155.         "Application Cache"
  156.     )
  157.  
  158.     $aggressiveTargets = @(
  159.         "File System",
  160.         "blob_storage",
  161.         "Service Worker\CacheStorage",
  162.         "Service Worker\ScriptCache",
  163.         "IndexedDB\https_*",
  164.         "Session Storage",
  165.         "Local Storage\leveldb"
  166.     )
  167.  
  168.     $targets = $safeTargets
  169.  
  170.     if ($Aggressive) {
  171.         $targets += $aggressiveTargets
  172.     }
  173.  
  174.     $locations = @()
  175.  
  176.     if (Test-Path $UserDataPath) {
  177.         $locations += $UserDataPath
  178.  
  179.         $locations += Get-ChildItem $UserDataPath -Directory -Force -ErrorAction SilentlyContinue |
  180.             Where-Object {
  181.                 $_.Name -eq "Default" -or
  182.                 $_.Name -like "Profile *" -or
  183.                 $_.Name -eq "Guest Profile" -or
  184.                 $_.Name -eq "System Profile"
  185.             } |
  186.             Select-Object -ExpandProperty FullName
  187.     }
  188.  
  189.     foreach ($loc in $locations) {
  190.         foreach ($target in $targets) {
  191.             Safe-Remove -Path (Join-Path $loc $target)
  192.         }
  193.     }
  194.  
  195.     $updatePath = Join-Path $RootPath "Update"
  196.  
  197.     if (Test-Path $updatePath) {
  198.         Get-ChildItem $updatePath -Recurse -Force -ErrorAction SilentlyContinue |
  199.             Where-Object {
  200.                 $_.Name -like "*.tmp" -or
  201.                 $_.Name -like "*.log" -or
  202.                 $_.Name -like "*.crdownload" -or
  203.                 $_.FullName -match "\\Download\\|\\Install\\|\\Temp\\"
  204.             } |
  205.             ForEach-Object {
  206.                 Safe-Remove -Path $_.FullName
  207.             }
  208.     }
  209.  
  210.     $after = Get-FolderSizeGB -Path $RootPath
  211.     Write-Host "$BrowserName size after : $after GB"
  212.     Write-Host "$BrowserName reduced by : $([math]::Round($before - $after, 2)) GB"
  213. }
  214.  
  215. function Backup-Firefox {
  216.     param(
  217.         [string]$FirefoxPath,
  218.         [string]$BackupRoot
  219.     )
  220.  
  221.     $profilesRoot = Join-Path $FirefoxPath "Profiles"
  222.  
  223.     if (!(Test-Path $profilesRoot)) {
  224.         return
  225.     }
  226.  
  227.     $firefoxBackup = Join-Path $BackupRoot "Firefox"
  228.     New-Item -ItemType Directory -Path $firefoxBackup -Force | Out-Null
  229.  
  230.     Backup-File -SourceFile (Join-Path $FirefoxPath "profiles.ini") -DestinationFolder $firefoxBackup
  231.     Backup-File -SourceFile (Join-Path $FirefoxPath "installs.ini") -DestinationFolder $firefoxBackup
  232.  
  233.     $profiles = Get-ChildItem $profilesRoot -Directory -Force -ErrorAction SilentlyContinue
  234.  
  235.     foreach ($profile in $profiles) {
  236.         $profileBackup = Join-Path $firefoxBackup $profile.Name
  237.         New-Item -ItemType Directory -Path $profileBackup -Force | Out-Null
  238.  
  239.         $importantFiles = @(
  240.             "places.sqlite",
  241.             "favicons.sqlite",
  242.             "key4.db",
  243.             "logins.json",
  244.             "cookies.sqlite",
  245.             "permissions.sqlite",
  246.             "prefs.js",
  247.             "formhistory.sqlite",
  248.             "cert9.db",
  249.             "handlers.json",
  250.             "extensions.json"
  251.         )
  252.  
  253.         foreach ($file in $importantFiles) {
  254.             Backup-File -SourceFile (Join-Path $profile.FullName $file) -DestinationFolder $profileBackup
  255.         }
  256.     }
  257. }
  258.  
  259. function Clean-Firefox {
  260.     param(
  261.         [string]$FirefoxPath,
  262.         [string]$BackupRoot
  263.     )
  264.  
  265.     if (!(Test-Path $FirefoxPath)) {
  266.         Write-Host "Firefox not found. Skipping."
  267.         return
  268.     }
  269.  
  270.     Write-Host ""
  271.     Write-Host "----------------------------------------"
  272.     Write-Host "Cleaning Firefox"
  273.     Write-Host "Path: $FirefoxPath"
  274.     Write-Host "----------------------------------------"
  275.  
  276.     $before = Get-FolderSizeGB -Path $FirefoxPath
  277.     Write-Host "Firefox size before: $before GB"
  278.  
  279.     taskkill /IM firefox.exe /F 2>$null | Out-Null
  280.  
  281.     Backup-Firefox -FirefoxPath $FirefoxPath -BackupRoot $BackupRoot
  282.  
  283.     $profilesRoot = Join-Path $FirefoxPath "Profiles"
  284.  
  285.     if (Test-Path $profilesRoot) {
  286.         $profiles = Get-ChildItem $profilesRoot -Directory -Force -ErrorAction SilentlyContinue
  287.  
  288.         foreach ($profile in $profiles) {
  289.             $safeTargets = @(
  290.                 "cache2",
  291.                 "startupCache",
  292.                 "shader-cache",
  293.                 "jumpListCache",
  294.                 "thumbnails",
  295.                 "minidumps",
  296.                 "datareporting",
  297.                 "crashes",
  298.                 "saved-telemetry-pings"
  299.             )
  300.  
  301.             $aggressiveTargets = @(
  302.                 "storage\default\https+++*",
  303.                 "storage\temporary",
  304.                 "storage\ls-archive.sqlite",
  305.                 "sessionstore-backups"
  306.             )
  307.  
  308.             $targets = $safeTargets
  309.  
  310.             if ($Aggressive) {
  311.                 $targets += $aggressiveTargets
  312.             }
  313.  
  314.             foreach ($target in $targets) {
  315.                 Safe-Remove -Path (Join-Path $profile.FullName $target)
  316.             }
  317.         }
  318.     }
  319.  
  320.     $after = Get-FolderSizeGB -Path $FirefoxPath
  321.     Write-Host "Firefox size after : $after GB"
  322.     Write-Host "Firefox reduced by : $([math]::Round($before - $after, 2)) GB"
  323. }
  324.  
  325. $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
  326. $backupRoot = Join-Path $env:USERPROFILE "Desktop\Browser_Backup_Important_$timestamp"
  327.  
  328. New-Item -ItemType Directory -Path $backupRoot -Force | Out-Null
  329.  
  330. Write-Host "Backup folder:"
  331. Write-Host $backupRoot
  332. Write-Host ""
  333.  
  334. $browserList = @(
  335.     @{
  336.         Name = "Chrome"
  337.         Root = Join-Path $env:LOCALAPPDATA "Google"
  338.         UserData = Join-Path $env:LOCALAPPDATA "Google\Chrome\User Data"
  339.         Process = "chrome.exe"
  340.     },
  341.     @{
  342.         Name = "Edge"
  343.         Root = Join-Path $env:LOCALAPPDATA "Microsoft\Edge"
  344.         UserData = Join-Path $env:LOCALAPPDATA "Microsoft\Edge\User Data"
  345.         Process = "msedge.exe"
  346.     },
  347.     @{
  348.         Name = "Brave"
  349.         Root = Join-Path $env:LOCALAPPDATA "BraveSoftware"
  350.         UserData = Join-Path $env:LOCALAPPDATA "BraveSoftware\Brave-Browser\User Data"
  351.         Process = "brave.exe"
  352.     }
  353. )
  354.  
  355. $totalBefore = 0
  356. $totalAfter = 0
  357.  
  358. foreach ($browser in $browserList) {
  359.     if (Test-Path $browser.Root) {
  360.         $totalBefore += Get-FolderSizeGB -Path $browser.Root
  361.     }
  362. }
  363.  
  364. $firefoxPath = Join-Path $env:APPDATA "Mozilla\Firefox"
  365.  
  366. if (Test-Path $firefoxPath) {
  367.     $totalBefore += Get-FolderSizeGB -Path $firefoxPath
  368. }
  369.  
  370. foreach ($browser in $browserList) {
  371.     Clean-Chromium-Browser `
  372.         -BrowserName $browser.Name `
  373.         -RootPath $browser.Root `
  374.         -UserDataPath $browser.UserData `
  375.         -ProcessName $browser.Process `
  376.         -BackupRoot $backupRoot
  377. }
  378.  
  379. Clean-Firefox -FirefoxPath $firefoxPath -BackupRoot $backupRoot
  380.  
  381. foreach ($browser in $browserList) {
  382.     if (Test-Path $browser.Root) {
  383.         $totalAfter += Get-FolderSizeGB -Path $browser.Root
  384.     }
  385. }
  386.  
  387. if (Test-Path $firefoxPath) {
  388.     $totalAfter += Get-FolderSizeGB -Path $firefoxPath
  389. }
  390.  
  391. Write-Host ""
  392. Write-Host "========================================"
  393. Write-Host " Cleanup Completed"
  394. Write-Host "========================================"
  395. Write-Host "Total browser size before: $([math]::Round($totalBefore, 2)) GB"
  396. Write-Host "Total browser size after : $([math]::Round($totalAfter, 2)) GB"
  397. Write-Host "Total space reduced      : $([math]::Round($totalBefore - $totalAfter, 2)) GB"
  398. Write-Host ""
  399. Write-Host "Backup created at:"
  400. Write-Host $backupRoot
  401. Write-Host ""
  402. Write-Host "Passwords, bookmarks, settings, extensions, history, and cookies were not intentionally deleted."
  403. Write-Host ""
  404. Write-Host "Note:"
  405. Write-Host "Chromium passwords are tied to Windows DPAPI and Local State."
  406. Write-Host "Firefox passwords are backed up using logins.json and key4.db."
  407. Write-Host "Use backup only on the same Windows user profile/machine unless you know what you are doing."
  408. Write-Host "========================================"
Advertisement
Add Comment
Please, Sign In to add comment