@@ -334,23 +334,16 @@ Create-File -fileContent $notification -fileName 'Enable - Notifications and Bac
334334# Optimizations
335335# Cleanup
336336$cleanup = @'
337- # Ensure the script runs as Administrator
338- if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
339- Write-Output "Please run this script as Administrator."
340- Pause
341- Exit
342- }
343-
344337# Kill Process, Ignore Errors and Continue
345338$ErrorActionPreference = 'SilentlyContinue'
346339Get-Process -Name cleanmgr -EA 0 | Stop-Process -Force -EA 0
347340
348341# Function to display sections
349342function Show-Message ($message) {
350- Write-Output "==========================================="
351- Write-Output " $message"
352- Write-Output "==========================================="
353- Start-Sleep -Seconds 2
343+ Write-Output "==========================================="
344+ Write-Output " $message"
345+ Write-Output "==========================================="
346+ Start-Sleep -Seconds 1
354347}
355348
356349# Show initial disk space
@@ -359,96 +352,95 @@ $initialFreeGB = [math]::Round($initialFreeMB / 1024, 2)
359352
360353Show-Message "Using Disk Cleanup with custom configuration"
361354$volumeCache = @{
362- "Active Setup Temp Folders" = 2
363- "BranchCache" = 2
364- "Content Indexer Cleaner" = 2
365- "Delivery Optimization Files" = 2
366- "Device Driver Packages" = 2
367- "Diagnostic Data Viewer database files" = 2
368- "Downloaded Program Files" = 2
369- "Feedback Hub Archive log files" = 2
370- "Internet Cache Files" = 2
371- "Language Pack" = 2
372- "Offline Pages Files" = 2
373- "Old ChkDsk Files" = 2
374- "Recycle Bin" = 2
375- "RetailDemo Offline Content" = 2
376- "Setup Log Files" = 2
377- "System error memory dump files" = 2
378- "System error minidump files" = 2
379- "Temporary Files" = 2
380- "Temporary Setup Files" = 2
381- "Temporary Sync Files" = 2
382- "Update Cleanup" = 2
383- "Upgrade Discarded Files" = 2
384- "User file versions" = 2
385- "Windows Defender" = 2
386- "Windows Error Reporting Files" = 2
387- "Windows ESD installation files" = 2
388- "Windows Reset Log Files" = 2
389- "Windows Upgrade Log Files" = 2
355+ "Active Setup Temp Folders" = 2
356+ "BranchCache" = 2
357+ "Content Indexer Cleaner" = 2
358+ "Delivery Optimization Files" = 2
359+ "Device Driver Packages" = 2
360+ "Diagnostic Data Viewer database files" = 2
361+ "Downloaded Program Files" = 2
362+ "Feedback Hub Archive log files" = 2
363+ "Internet Cache Files" = 2
364+ "Language Pack" = 2
365+ "Offline Pages Files" = 2
366+ "Old ChkDsk Files" = 2
367+ "Recycle Bin" = 2
368+ "RetailDemo Offline Content" = 2
369+ "Setup Log Files" = 2
370+ "System error memory dump files" = 2
371+ "System error minidump files" = 2
372+ "Temporary Files" = 2
373+ "Temporary Setup Files" = 2
374+ "Temporary Sync Files" = 2
375+ "Update Cleanup" = 2
376+ "Upgrade Discarded Files" = 2
377+ "User file versions" = 2
378+ "Windows Defender" = 2
379+ "Windows Error Reporting Files" = 2
380+ "Windows ESD installation files" = 2
381+ "Windows Reset Log Files" = 2
382+ "Windows Upgrade Log Files" = 2
390383}
391384$registryPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
392385foreach ($item in $volumeCache.GetEnumerator()) {
393- $keyPath = Join-Path $registryPath $item.Key
394- if (Test-Path $keyPath) {
395- New-ItemProperty -Path $keyPath -Name StateFlags1337 -Value $item.Value -PropertyType DWord | Out-Null
396- }
386+ $keyPath = Join-Path $registryPath $item.Key
387+ if (Test-Path $keyPath) {
388+ New-ItemProperty -Path $keyPath -Name StateFlags1337 -Value $item.Value -PropertyType DWord | Out-Null
389+ }
397390}
398391Start-Process -FilePath "$env:SystemRoot\system32\cleanmgr.exe" -ArgumentList "/sagerun:1337" -Wait:$false
399392
400393Show-Message "Cleaning up leftovers"
401394$foldersToRemove = @(
402- "CbsTemp",
403- "Logs",
404- "Panther",
405- "Prefetch",
406- "SoftwareDistribution",
407- "System32\LogFiles",
408- "System32\LogFiles\WMI",
409- "System32\SleepStudy",
410- "System32\sru",
411- "System32\WDI\LogFiles",
412- "System32\winevt\Logs",
413- "SystemTemp",
414- "Temp"
395+ "C:\CbsTemp",
396+ "C:\Logs",
397+ "C:\Panther",
398+ "C:\Prefetch",
399+ "C:\ProgramData\Microsoft\Windows Defender\Scans",
400+ "C:\Program Files (x86)\Microsoft\EdgeUpdate\Download",
401+ "C:\SoftwareDistribution",
402+ "C:\System32\LogFiles",
403+ "C:\System32\LogFiles\WMI",
404+ "C:\System32\SleepStudy",
405+ "C:\System32\sru",
406+ "C:\System32\WDI\LogFiles",
407+ "C:\System32\winevt\Logs",
408+ "C:\SystemTemp",
409+ "C:\Temp"
415410)
416411
417- foreach ($folderName in $foldersToRemove) {
418- $folderPath = Join-Path $env:SystemRoot $folderName
419- if (Test-Path $folderPath) {
420- Remove-Item -Path "$folderPath\*" -Force -Recurse | Out-Null
412+ foreach ($folderPath in $foldersToRemove) {
413+ if (Test-Path $folderPath) {
414+ try {
415+ & takeown /F $folderPath /R /D Y *> $null
416+ & icacls $folderPath /grant Administrators:F /T *> $null
417+ Write-Output " - Deleted Files from Folder: $($folderPath)"
418+ Remove-Item -Path "$folderPath\*" -Force -Recurse
421419 }
420+ catch {}
421+ }
422+ }
423+ $fileTypes = @("C:\*.tmp", "C:\*.log", "C:\*.chk", "C:\*.old")
424+ foreach ($path in $fileTypes) {
425+ Get-ChildItem -Path $path -Recurse -Force | ForEach-Object {
426+ try {
427+ Remove-Item -Path $_.FullName -Force -Recurse
428+ Write-Output " - Deleted File: $($_.FullName)"
429+ } catch {}
430+ }
422431}
423- Remove-Item -Path "C:\Program Files\WindowsApps\MicrosoftWindows.Client.WebExperience*" -Recurse -Force
424-
425- Show-Message "Cleaning up %TEMP% and Log Files"
426- Get-ChildItem -Path "$env:TEMP" | Remove-Item -Recurse -Force
427- Get-ChildItem -Path "$env:SystemRoot" -Filter *.log -File -Recurse -Force | Remove-Item -Recurse -Force | Out-Null
428-
429- Show-Message "Cleaning up Event Logs"
430- Get-EventLog -LogName * | ForEach-Object { Clear-EventLog $_.Log }
431432
432433# Run DISM Component Cleanup
433434Show-Message "Cleaning up WinSxS components"
434435Dism /online /Cleanup-Image /StartComponentCleanup /ResetBase
435436
436- # Clean Edge Update Downloads
437- Show-Message "Cleaning up EdgeUpdate Downloads"
438- $edgeUpdatePath = ${env:ProgramFiles(x86)} + "\Microsoft\EdgeUpdate\Download"
439- if (Test-Path -Path $edgeUpdatePath) {
440- Remove-Item -Path $edgeUpdatePath -Force -Recurse | Out-Null
441- }
442-
443437# Clean Empty Folders
444438Show-Message "Cleaning Empty Folders"
445- Get-ChildItem -Directory -Path "C:\" -Recurse -ErrorAction SilentlyContinue |
446- Where-Object { try { ($_.GetFiles().Count -eq 0) -and ($_.GetDirectories().Count -eq 0) } catch { $false } } |
447- ForEach-Object {
448- try {
449- Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue
450- Write-Output "Deleted Empty Folder: $($_.FullName)"
451- } catch {}
439+ Get-ChildItem -Directory -Path "C:\" -Recurse | Where-Object { try { ($_.GetFiles().Count -eq 0) -and ($_.GetDirectories().Count -eq 0) } catch { $false } } | ForEach-Object {
440+ try {
441+ Remove-Item $_.FullName -Force -Recurse
442+ Write-Output " - Deleted Empty Folder: $($_.FullName)"
443+ } catch {}
452444}
453445
454446# Clean System Restore Points
@@ -460,11 +452,9 @@ $finalFreeMB = (Get-PSDrive -Name C).Free / 1MB
460452$finalFreeGB = [math]::Round($finalFreeMB / 1024, 2)
461453$freedSpaceMB = $finalFreeMB - $initialFreeMB
462454$freedSpaceGB = [math]::Round($freedSpaceMB / 1024, 2)
463-
464455Show-Message "Before Cleanup: $initialFreeMB MB ($initialFreeGB GB) Free"
465456Show-Message "After Cleanup: $finalFreeMB MB ($finalFreeGB GB) Free"
466457Show-Message "Freed Space: $freedSpaceMB MB ($freedSpaceGB GB)"
467-
468458Pause
469459'@
470460Create- File - fileContent $cleanup - fileName ' Cleanup.ps1' - fileDirectory $optimizationPath
@@ -496,19 +486,11 @@ pause & exit
496486set reg1="HKCU\System\GameConfigStore"
497487set reg2="HKLM\SOFTWARE\Policies\Microsoft\Windows\GameDVR"
498488if "%~n0"=="Enable - GameDVR" (
499- reg add %reg1% /v GameDVR_FSEBehavior /t REG_DWORD /d 0 /f
500489reg add %reg1% /v GameDVR_Enabled /t REG_DWORD /d 1 /f
501- reg add %reg1% /v GameDVR_DXGIHonorFSEWindowsCompatible /t REG_DWORD /d 0 /f
502- reg add %reg1% /v GameDVR_HonorUserFSEBehaviorMode /t REG_DWORD /d 0 /f
503- reg add %reg1% /v GameDVR_EFSEFeatureFlags /t REG_DWORD /d 1 /f
504490reg add %reg2% /v AllowGameDVR /t REG_DWORD /d 1 /f
505491 ren "%~dpnx0" "Disable - GameDVR.cmd"
506492) else (
507- reg delete %reg1% /v GameDVR_FSEBehavior /f
508493reg delete %reg1% /v GameDVR_Enabled /f
509- reg delete %reg1% /v GameDVR_DXGIHonorFSEWindowsCompatible /f
510- reg delete %reg1% /v GameDVR_HonorUserFSEBehaviorMode /f
511- reg delete %reg1% /v GameDVR_EFSEFeatureFlags /f
512494reg delete %reg2% /v AllowGameDVR /f
513495ren "%~dpnx0" "Enable - GameDVR.cmd"
514496)
0 commit comments