Windows Ultra Optimization V2

Advanced PowerShell Script for Cleaning and Performance (SSD-Safe)

PowerShell Script

#------------------------------------------------------------------------------------
# Windows Ultra Optimization V2 - Safe for SSDs
# Author: Gemini AI
# Instructions:
# 1. ALWAYS create a System Restore point first.
# 2. Run this entire script in an elevated PowerShell (as Administrator).
#------------------------------------------------------------------------------------

# --- SCRIPT CONFIGURATION ---
$DaysOldToDelete = 7 # Files older than this many days will be deleted from temp folders.
$KeepRecentRestorePoints = 1 # How many of the latest system restore points to keep.

# --- HELPER FUNCTIONS ---
function Write-SectionHeader {
    param([string]$Title)
    Write-Host "`n"
    Write-Host "========================================================================" -ForegroundColor Cyan
    Write-Host "  $Title" -ForegroundColor Cyan
    Write-Host "========================================================================" -ForegroundColor Cyan
}

# --- SCRIPT START ---
Write-Host "Starting Windows Ultra Optimization V2..." -ForegroundColor Green
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Force

#------------------------------------------------------------------------------------
# SECTION 1: DEEP FILE & CACHE CLEANUP
#------------------------------------------------------------------------------------
Write-SectionHeader -Title "Deep File & Cache Cleanup"

# Stop services to release file locks, restart them later
$servicesToManage = @("wuauserv", "BITS", "Spooler")
Write-Host "Stopping services to unlock files..."
$servicesToManage | ForEach-Object { Stop-Service -Name $_ -Force -ErrorAction SilentlyContinue }

# Extensive list of temporary and cache folders
$cleanupPaths = @(
    "$env:TEMP",
    "$env:SystemRoot\Temp",
    "$env:LOCALAPPDATA\Temp",
    "$env:USERPROFILE\AppData\Local\Temp",
    "$env:SystemRoot\SoftwareDistribution\Download",
    "$env:LOCALAPPDATA\Microsoft\Windows\INetCache",
    "$env:LOCALAPPDATA\Microsoft\Windows\WebCache",
    "$env:LOCALAPPDATA\CrashDumps",
    "$env:SystemRoot\Prefetch",
    "$env:LOCALAPPDATA\Microsoft\Windows\Explorer",
    "$env:SystemRoot\Logs",
    "C:\Users\*\AppData\Local\Temp" # For all users
)

Write-Host "Cleaning temporary files older than $DaysOldToDelete days..."
foreach ($path in $cleanupPaths) {
    if (Test-Path $path) {
        Get-ChildItem -Path $path -Recurse -File -Force -ErrorAction SilentlyContinue | 
        Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$DaysOldToDelete) } | 
        Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
        Write-Host "  - Cleaned: $path"
    }
}

# Specific cache locations
Write-Host "Cleaning browser and application caches..."
Remove-Item "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache\*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$env:LOCALAPPDATA\Mozilla\Firefox\Profiles\*\cache2\entries\*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache\*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$env:LOCALAPPDATA\NVIDIA\GLCache\*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$env:LOCALAPPDATA\Discord\Cache\*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$env:APPDATA\Steam\appcache\httpcache\*" -Recurse -Force -ErrorAction SilentlyContinue

# Clear Windows Update and Delivery Optimization caches
Write-Host "Clearing Windows Update & Delivery Optimization cache..."
Remove-Item "$env:SystemRoot\SoftwareDistribution\DataStore\Logs\*" -Force -ErrorAction SilentlyContinue
Remove-Item "$env:SystemDrive\ProgramData\Microsoft\Network\Downloader\*" -Recurse -Force -ErrorAction SilentlyContinue

# Clear system caches
Write-Host "Clearing system caches (DNS, Icons, Thumbnails)..."
Clear-DnsClientCache
Remove-Item "$env:LOCALAPPDATA\Microsoft\Windows\Explorer\thumbcache_*.db" -Force -ErrorAction SilentlyContinue
if (Test-Path "$env:LOCALAPPDATA\IconCache.db") { Remove-Item "$env:LOCALAPPDATA\IconCache.db" -Force -ErrorAction SilentlyContinue }

# Clear Print Spooler and Catroot2
Write-Host "Clearing Print Spooler and Catroot2..."
Remove-Item "$env:SystemRoot\System32\spool\PRINTERS\*" -Force -Recurse -ErrorAction SilentlyContinue
net stop cryptsvc
Remove-Item "$env:SystemRoot\System32\catroot2\*" -Recurse -Force -ErrorAction SilentlyContinue
net start cryptsvc

# Clear Recycle Bin
Write-Host "Emptying Recycle Bin..."
Clear-RecycleBin -Force -ErrorAction SilentlyContinue

# Restart services
Write-Host "Restarting services..."
$servicesToManage | ForEach-Object { Start-Service -Name $_ -ErrorAction SilentlyContinue }

#------------------------------------------------------------------------------------
# SECTION 2: SYSTEM MAINTENANCE & OPTIMIZATION (SSD-SAFE)
#------------------------------------------------------------------------------------
Write-SectionHeader -Title "System Maintenance & Optimization"

# Clean up old System Restore points, keeping the most recent ones
Write-Host "Cleaning old System Restore points..."
$restorePoints = Get-ComputerRestorePoint
if ($restorePoints.Count -gt $KeepRecentRestorePoints) {
    $restorePoints | Sort-Object -Property CreationTime | Select-Object -First ($restorePoints.Count - $KeepRecentRestorePoints) | ForEach-Object {
        Write-Host "  - Deleting restore point: $($_.Description)"
        (Get-WmiObject -Namespace root/default -Class SystemRestore).DeleteRestorePoint($_.SequenceNumber)
    }
}

# Clean up WinSxS (Component Store)
Write-Host "Cleaning up Component Store (WinSxS). This may take a while..."
Dism.exe /Online /Cleanup-Image /StartComponentCleanup /Quiet
Write-Host "Analyzing Component Store..."
Dism.exe /Online /Cleanup-Image /AnalyzeComponentStore /Quiet

# Repair system image (safe operation)
Write-Host "Verifying and repairing system image health..."
Dism.exe /Online /Cleanup-Image /RestoreHealth /Quiet

# SSD & HDD Optimization
Write-Host "Optimizing drives (ReTrim for SSDs, Defrag for HDDs)..."
Get-WmiObject -Class Win32_Volume | ForEach-Object {
    $driveLetter = $_.DriveLetter
    if ($driveLetter) {
        Optimize-Volume -DriveLetter $driveLetter.Substring(0,1) -ReTrim -ErrorAction SilentlyContinue
    }
}

# Clear all Windows Event Logs
Write-Host "Clearing all Windows Event Logs..."
wevtutil el | ForEach-Object { wevtutil cl "$_" }

#------------------------------------------------------------------------------------
# SECTION 3: DEBLOAT & UNINSTALL (SAFE REMOVALS)
#------------------------------------------------------------------------------------
Write-SectionHeader -Title "Debloating Windows"

# List of common bloatware UWP apps (safe to remove)
$bloatwareApps = @(
    "Microsoft.549981C3F5F10", # Cortana
    "Microsoft.BingNews",
    "Microsoft.BingWeather",
    "Microsoft.GetHelp",
    "Microsoft.Getstarted",
    "Microsoft.Messaging",
    "Microsoft.Microsoft3DViewer",
    "Microsoft.MicrosoftOfficeHub",
    "Microsoft.MicrosoftSolitaireCollection",
    "Microsoft.MixedReality.Portal",
    "Microsoft.Office.OneNote",
    "Microsoft.OneConnect", # Print 3D
    "Microsoft.People",
    "Microsoft.SkypeApp",
    "Microsoft.StorePurchaseApp",
    "Microsoft.Wallet",
    "Microsoft.WindowsAlarms",
    "Microsoft.WindowsFeedbackHub",
    "Microsoft.WindowsMaps",
    "Microsoft.WindowsSoundRecorder",
    "Microsoft.YourPhone",
    "Microsoft.ZuneMusic",
    "Microsoft.ZuneVideo",
    "Microsoft.XboxApp",
    "Microsoft.XboxGameOverlay",
    "Microsoft.XboxGamingOverlay",
    "Microsoft.XboxSpeechToTextOverlay"
)

Write-Host "Removing pre-installed bloatware apps..."
foreach ($app in $bloatwareApps) {
    Get-AppxPackage -Name $app -AllUsers | ForEach-Object {
        Write-Host "  - Removing: $($_.Name)"
        Remove-AppxPackage -Package $_.PackageFullName -AllUsers -ErrorAction SilentlyContinue
    }
    Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -eq $app } | ForEach-Object {
        Write-Host "  - Removing provisioned package: $($_.DisplayName)"
        Remove-AppxProvisionedPackage -Online -PackageName $_.PackageName -ErrorAction SilentlyContinue
    }
}

#------------------------------------------------------------------------------------
# SECTION 4: PERFORMANCE & PRIVACY TWEAKS
#------------------------------------------------------------------------------------
Write-SectionHeader -Title "Applying Performance & Privacy Tweaks"

# Set power plan to Ultimate Performance (if available) or High Performance
Write-Host "Setting power plan for maximum performance..."
$ultimatePlan = powercfg -l | Select-String "Ultimate Performance"
if ($ultimatePlan) {
    $guid = ($ultimatePlan -split " ")[3]
    powercfg -setactive $guid
    Write-Host "  - Ultimate Performance plan activated."
} else {
    $highPerfPlan = powercfg -l | Select-String "High performance"
    $guid = ($highPerfPlan -split " ")[3]
    powercfg -setactive $guid
    Write-Host "  - High Performance plan activated."
}

# Disable unnecessary services for performance and privacy
Write-Host "Disabling non-essential services..."
$servicesToDisable = @(
    "Fax",                # Used for sending/receiving faxes
    "WerSvc",             # Windows Error Reporting
    "wisvc",              # Windows Insider Service
    "DiagTrack",          # Connected User Experiences and Telemetry
    "dmwappushservice",   # WAP Push Message Routing Service
    "MapsBroker",         # Downloaded Maps Manager
    "RetailDemo",         # Retail Demo Service
    "XblGameSave",        # Xbox Live Game Save
    "PhoneSvc"            # Phone Service
)
foreach ($svc in $servicesToDisable) {
    if (Get-Service -Name $svc -ErrorAction SilentlyContinue) {
        Set-Service -Name $svc -StartupType Disabled -ErrorAction SilentlyContinue
        Stop-Service -Name $svc -Force -ErrorAction SilentlyContinue
        Write-Host "  - Disabled service: $svc"
    }
}

# Adjust visual effects for best performance
Write-Host "Setting visual effects for best performance..."
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\VisualEffects" -Name "VisualFxSetting" -Value 2 -Force -ErrorAction SilentlyContinue
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "AutoEndTasks" -Value "1" -Force -ErrorAction SilentlyContinue
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "MenuShowDelay" -Value "100" -Force -ErrorAction SilentlyContinue
Set-ItemProperty -Path "HKCU:\Control Panel\Mouse" -Name "MouseHoverTime" -Value "100" -Force -ErrorAction SilentlyContinue

# Disable suggested content and tips
Write-Host "Disabling ads and suggested content..."
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "SubscribedContent-338388Enabled" -Value 0 -Force -ErrorAction SilentlyContinue
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "ShowSyncProviderNotifications" -Value 0 -Force -ErrorAction SilentlyContinue

#------------------------------------------------------------------------------------
# SCRIPT COMPLETION
#------------------------------------------------------------------------------------
Write-Host "`n"
Write-Host "========================================================================" -ForegroundColor Green
Write-Host "  Ultra Optimization Complete!" -ForegroundColor Green
Write-Host "  A system restart is recommended to apply all changes." -ForegroundColor Green
Write-Host "========================================================================" -ForegroundColor Green