$WScriptShell = New-Object -ComObject WScript.Shell $shortcut = $WScriptShell.CreateShortcut($shortcutPath) $shortcut.TargetPath = "powershell.exe" $shortcut.Arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`"" $shortcut.WorkingDirectory = Split-Path $scriptPath $shortcut.IconLocation = "shell32.dll,24" $shortcut.Description = "Clear Windows 11 Temporary Files" $shortcut.Save()
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`" -Silent" $trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 3am $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest windows 11 clear temp files
foreach ($loc in $locations) if ($loc.Path -eq "RecycleBin") # Handle Recycle Bin separately $size = Get-RecycleBinSize if (-not $Silent) Write-Host "Checking $($loc.Name)..." -ForegroundColor Yellow if (-not $Silent) Write-Host " Size: $(Format-FileSize $size)" -ForegroundColor Gray if ($size -gt 0 -and (-not $Silent)) $confirm = Read-Host " Clear Recycle Bin? (Y/N)" if ($confirm -eq 'Y') Clear-RecycleBin -Force -ErrorAction SilentlyContinue $totalFreed += $size $results += [PSCustomObject]@Location = $loc.Name; Freed = $size if (-not $Silent) Write-Host " ✓ Cleared $(Format-FileSize $size)" -ForegroundColor Green elseif ($size -gt 0 -and $Silent) Clear-RecycleBin -Force -ErrorAction SilentlyContinue $totalFreed += $size elseif (Test-Path $loc.Path) $size = Get-FolderSize $loc.Path if (-not $Silent) Write-Host "Checking $($loc.Name)..." -ForegroundColor Yellow if (-not $Silent) Write-Host " Size: $(Format-FileSize $size)" -ForegroundColor Gray if ($size -gt 0 -and (-not $Silent)) $confirm = Read-Host " Clear this folder? (Y/N/A - All)" if ($confirm -eq 'Y' -or $confirm -eq 'A') $freed = Clean-Folder $loc.Path $totalFreed += $freed $results += [PSCustomObject]@Location = $loc.Name; Freed = $freed if (-not $Silent) Write-Host " ✓ Cleared $(Format-FileSize $freed)" -ForegroundColor Green if ($confirm -eq 'A') $Silent = $true elseif ($size -gt 0 -and $Silent) $freed = Clean-Folder $loc.Path $totalFreed += $freed else if (-not $Silent) Write-Host "Skipping $($loc.Name) (not found)" -ForegroundColor DarkGray if (-not $Silent) Write-Host "" $WScriptShell = New-Object -ComObject WScript
echo Emptying recycle bin... powershell.exe -Command "Clear-RecycleBin -Force" powershell
return $totalFreed
# Run in PowerShell as Administrator Get-ChildItem -Path "$env:TEMP","$env:SystemRoot\Temp","$env:SystemRoot\Prefetch" -Recurse -Force -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue; Clear-RecycleBin -Force Method 3: Batch File (Traditional) @echo off title Windows 11 Temp Cleaner color 0A echo ======================================== echo Windows 11 Temporary Files Cleaner echo ======================================== echo. echo Cleaning user temp... del /q /f /s "%TEMP%*" > nul 2>&1 rmdir /q /s "%TEMP%" > nul 2>&1 mkdir "%TEMP%" > nul 2>&1