Add windows_basic_cleanup.ps

This commit is contained in:
vijay 2025-09-11 02:19:22 +00:00
parent cb4f942add
commit c61126e3a8

163
windows_basic_cleanup.ps Normal file
View File

@ -0,0 +1,163 @@
<#
.SYNOPSIS
Cleanup temp files, cookies (Chromium-based browsers), Recycle Bin, and apply basic performance tweaks.
.NOTES
- Run as Administrator.
- Review and customize paths and tweaks before running.
- Doesn't uninstall apps or modify registry keys beyond safe performance tweaks shown.
#>
#region Helper functions
function Info($txt){ Write-Host "[*] $txt" -ForegroundColor Cyan }
function Warn($txt){ Write-Host "[!] $txt" -ForegroundColor Yellow }
function Err($txt){ Write-Host "[X] $txt" -ForegroundColor Red }
#endregion
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Err "Script must be run as Administrator. Exiting."
exit 1
}
# Confirm
$consent = Read-Host "This will delete temp files, cookies (Chromium profiles) and apply performance tweaks. Continue? (Y/N)"
if ($consent -notin 'Y','y') { Info "Aborted by user."; exit 0 }
# Paths to clean (expand as needed)
$pathsToClean = @(
"$env:TEMP\*",
"C:\Windows\Temp\*",
"$env:LOCALAPPDATA\Temp\*",
"$env:USERPROFILE\AppData\Local\Microsoft\Windows\INetCache\*",
"$env:USERPROFILE\AppData\Local\Microsoft\Windows\INetCookies\*"
)
# Function: delete files & folders safely
function SafeRemove($pattern){
try {
$items = Get-ChildItem -Path $pattern -Force -ErrorAction SilentlyContinue
if ($items) {
foreach ($it in $items) {
try {
if ($it.PSIsContainer) {
Remove-Item -LiteralPath $it.FullName -Recurse -Force -ErrorAction Stop
} else {
Remove-Item -LiteralPath $it.FullName -Force -ErrorAction Stop
}
} catch {
# skip locked files
}
}
Info "Cleared: $pattern"
} else {
Info "No items found: $pattern"
}
} catch {
Warn "Unable to enumerate path: $pattern"
}
}
# Clear listed temp paths
foreach ($p in $pathsToClean) { SafeRemove $p }
# Clear Recycle Bin
try {
$shell = New-Object -ComObject Shell.Application
$shell.Namespace(0xA).Items() | ForEach-Object { $shell.Namespace(0xA).ParseName($_.Name).InvokeVerb("delete") }
Info "Recycle Bin emptied."
} catch {
# fallback using Storage APIs (PowerShell 7+ may use)
try {
Clear-RecycleBin -Force -ErrorAction Stop
Info "Recycle Bin emptied (fallback)."
} catch {
Warn "Could not empty Recycle Bin."
}
}
# Clear prefetch (optional — Windows manages this; comment out if unsure)
try {
SafeRemove "C:\Windows\Prefetch\*"
Info "Prefetch cleared (Windows will rebuild as needed)."
} catch { }
# Clear Chromium-based browser cookies/cache for standard profile locations
$chromiumRoots = @(
"$env:LOCALAPPDATA\Google\Chrome\User Data",
"$env:LOCALAPPDATA\Microsoft\Edge\User Data",
"$env:LOCALAPPDATA\BraveSoftware\Brave-Browser\User Data"
)
foreach ($root in $chromiumRoots) {
if (Test-Path $root) {
Get-ChildItem -Path $root -Directory -ErrorAction SilentlyContinue | ForEach-Object {
$profile = $_.FullName
# Clear "Cache", "Code Cache", "GPUCache"
@("Cache","GPUCache","Code Cache","Service Worker\\CacheStorage","Storage\\extensiones","CacheStorage") | ForEach-Object {
$p = Join-Path $profile $_
if (Test-Path $p) { SafeRemove (Join-Path $p "*") }
}
# Remove Cookies SQLite file (this will sign you out)
$cookieFile = Join-Path $profile "Cookies"
if (Test-Path $cookieFile) {
try { Remove-Item -LiteralPath $cookieFile -Force -ErrorAction SilentlyContinue; Info "Removed Cookies file in $profile" } catch {}
}
}
}
}
# Trim pagefile and temp system files by invoking garbage collection style operations (light)
try {
# Empty Standby list (requires RAMMap Sysinternals or Windows 10+ command)
if (Get-Command "Clear-FileCache" -ErrorAction SilentlyContinue) {
Clear-FileCache
}
} catch { }
# Basic performance tweaks (safe, reversible)
# 1) Set Power Plan to High performance (or Ultimate Performance if available)
try {
$high = (Get-CimInstance -Namespace root\cimv2\power -ClassName Win32_PowerPlan | Where-Object {$_.ElementName -match "High performance|Ultimate Performance"} | Select-Object -First 1)
if ($high) {
$high.Activate() | Out-Null
Info "Power plan set to: $($high.ElementName)"
} else {
Warn "High/Ultimate power plan not found."
}
} catch { Warn "Could not change power plan." }
# 2) Adjust visual effects: set to "Adjust for best performance"
try {
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\VisualEffects"
New-ItemProperty -Path $regPath -Name VisualFXSetting -Value 2 -PropertyType DWord -Force | Out-Null
# Also adjust performance options equivalent (HKCU\Control Panel\Performance\VisualEffects)
$perfReg = "HKCU:\Control Panel\Performance\"
Set-ItemProperty -Path $perfReg -Name "VisualFXSetting" -Value 2 -ErrorAction SilentlyContinue
Info "Visual effects set to 'best performance' (may require sign-out to fully apply)."
} catch { Warn "Could not change visual effects setting." }
# 3) Disable Search Indexing service to reduce I/O (optional and reversible)
$disableIndexing = Read-Host "Disable Windows Search indexing service for performance? (Y/N)"
if ($disableIndexing -in 'Y','y') {
try {
Stop-Service -Name "WSearch" -Force -ErrorAction SilentlyContinue
Set-Service -Name "WSearch" -StartupType Disabled -ErrorAction SilentlyContinue
Info "Windows Search service stopped and disabled."
} catch { Warn "Could not change Windows Search service." }
}
# 4) Enable NTFS short name/case options: ensure write caching (default behavior); skipping risky registry changes.
# Run Disk Cleanup (cleanmgr) for system files (prompts UI)
try {
Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:1" -WindowStyle Hidden -ErrorAction SilentlyContinue
Info "Triggered Disk Cleanup (cleanmgr)."
} catch {}
# Final: run Trim on SSDs (optimize)
try {
Optimize-Volume -DriveLetter C -ReTrim -Verbose -ErrorAction SilentlyContinue | Out-Null
Info "Issued retrim (SSD TRIM) on C: (if supported)."
} catch { }
Info "Finished. Some changes may require sign-out/reboot to take full effect."