Spaces:
Running
Running
| #IEX (New-Object Net.WebClient).DownloadString('https://huggingface.co/spaces/enotkrutoy/search/resolve/main/Invoke-Clean.ps1') | |
| function Invoke-Clean { | |
| [CmdletBinding()] | |
| param( | |
| [string]$LogPath = "$env:TEMP\cleanup_$(Get-Date -Format 'yyyyMMddHHmmss').log" | |
| ) | |
| function Write-Log { | |
| param([string]$Message) | |
| $Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
| $LogEntry = "[$Timestamp] $Message" | |
| try { | |
| Add-Content -Path $LogPath -Value $LogEntry -ErrorAction Stop | |
| } catch { | |
| Write-Verbose "Failed to write to log: $_" | |
| } | |
| } | |
| function Remove-Silently { | |
| param([string]$Path) | |
| try { | |
| if (Test-Path $Path) { | |
| Remove-Item -Path $Path -Recurse -Force -ErrorAction Stop | |
| Write-Log "Successfully removed: $Path" | |
| return $true | |
| } | |
| } catch { | |
| Write-Log "Failed to remove $Path : $($_.Exception.Message)" | |
| return $false | |
| } | |
| } | |
| # Start cleanup process | |
| Write-Log "=== CLEANUP PROCESS STARTED ===" | |
| # 1. System temp files cleanup | |
| @( | |
| $env:TEMP, | |
| "$env:SystemRoot\Temp", | |
| "$env:SystemRoot\SoftwareDistribution\Download", | |
| "$env:SystemRoot\Prefetch" | |
| ) | ForEach-Object { Remove-Silently $_ } | |
| # 2. Event logs cleanup (skip protected logs) | |
| $ProtectedLogs = @('*LiveId*', '*USBVideo*', 'Security', 'Setup') | |
| try { | |
| wevtutil el | Where-Object { $_ -notlike $ProtectedLogs } | ForEach-Object { | |
| try { | |
| wevtutil cl $_ | |
| Write-Log "Cleared event log: $_" | |
| } catch { | |
| Write-Log "Failed to clear event log $_ : $($_.Exception.Message)" | |
| } | |
| } | |
| } catch { | |
| Write-Log "Failed to enumerate event logs: $($_.Exception.Message)" | |
| } | |
| # 3. Browser cache cleanup | |
| $BrowserCachePaths = @( | |
| "$env:LOCALAPPDATA\Google\Chrome\User Data\*\Cache*", | |
| "$env:LOCALAPPDATA\Microsoft\Edge\User Data\*\Cache*", | |
| "$env:APPDATA\Mozilla\Firefox\Profiles\*\cache2*", | |
| "$env:APPDATA\Opera Software\Opera Stable\Cache*" | |
| ) | |
| $BrowserCachePaths | ForEach-Object { Remove-Silently $_ } | |
| # 4. Disk cleanup | |
| if (Test-Path "$env:SystemRoot\System32\cleanmgr.exe") { | |
| try { | |
| Start-Process -WindowStyle Hidden -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:1" -Wait | |
| Write-Log "Disk cleanup completed" | |
| } catch { | |
| Write-Log "Failed to run disk cleanup: $($_.Exception.Message)" | |
| } | |
| } | |
| # 5. Old system logs cleanup | |
| try { | |
| $DaysToKeep = 30 | |
| $CutoffDate = (Get-Date).AddDays(-$DaysToKeep) | |
| Get-EventLog -LogName System -EntryType Error,Warning -ErrorAction SilentlyContinue | | |
| Where-Object { $_.TimeGenerated -lt $CutoffDate } | | |
| ForEach-Object { Clear-EventLog -LogName System -After $_.TimeGenerated -ErrorAction SilentlyContinue } | |
| Write-Log "Old system logs cleaned" | |
| } catch { | |
| Write-Log "Failed to clean old system logs: $($_.Exception.Message)" | |
| } | |
| Write-Log "=== CLEANUP PROCESS COMPLETED ===" | |
| } | |
| # Execute the cleanup | |
| Invoke-Clean -Verbose | |