diff --git a/src/Configuration/tweaks.yml b/src/Configuration/tweaks.yml index 44754a6960..d781f05e03 100644 --- a/src/Configuration/tweaks.yml +++ b/src/Configuration/tweaks.yml @@ -321,7 +321,6 @@ features: - tweaks\debloat\legacy-photo-viewer.yml - tweaks\debloat\prevent-edge-update.yml - tweaks\debloat\scheduled-tasks.yml - - tweaks\debloat\cleanup-temp-files.yml # ----------------------------------------------------- # Scripts @@ -334,6 +333,7 @@ features: - tweaks\statuses\status-scripts.yml - tweaks\scripts\script-storage-sense.yml - tweaks\scripts\script-devices.yml + - tweaks\scripts\script-cleanup.yml - tweaks\scripts\script-startmenu.yml - tweaks\scripts\script-pfp.yml - tweaks\scripts\script-wallpaper.yml diff --git a/src/Configuration/tweaks/debloat/cleanup-temp-files.yml b/src/Configuration/tweaks/debloat/cleanup-temp-files.yml deleted file mode 100644 index 032327e324..0000000000 --- a/src/Configuration/tweaks/debloat/cleanup-temp-files.yml +++ /dev/null @@ -1,18 +0,0 @@ ---- -title: Cleanup Temporary Files -description: Cleans up temporary files, excluding AME folders -privilege: TrustedInstaller -actions: - # Cleanmgr does not clean these up as AME needs to be prevented from being deleted - - !cmd: - command: 'for /f %a in (''dir /b "%TEMP%" /a:-d'') do del /q "%TEMP%\%a"' - weight: 10 - # Windows Temp folder - - !cmd: - command: 'del /f /s /q C:\Windows\Temp\*' - weight: 10 - # The AME folder has to be excluded here, otherwise it will remove - # the Playbook logs and cause issues with the AME Wizard - - !cmd: - command: 'for /f %a in (''dir /b "%TEMP%" /a:d ^| findstr /v /c:"AME"'') do rmdir /q /s "%TEMP%\%a"' - weight: 10 diff --git a/src/Configuration/tweaks/scripts/script-cleanup.yml b/src/Configuration/tweaks/scripts/script-cleanup.yml new file mode 100644 index 0000000000..4b764304f8 --- /dev/null +++ b/src/Configuration/tweaks/scripts/script-cleanup.yml @@ -0,0 +1,10 @@ +--- +title: Cleanup Temporary Files +description: Cleans up temporary files using Disk Cleanup (if no other installs of Windows are found) +privilege: TrustedInstaller +actions: + - !run: + exe: 'powershell.exe' + args: '-NoP -File CLEANUP.ps1' + exeDir: true + wait: true diff --git a/src/Executables/CLEANUP.ps1 b/src/Executables/CLEANUP.ps1 new file mode 100644 index 0000000000..90d2e106a3 --- /dev/null +++ b/src/Executables/CLEANUP.ps1 @@ -0,0 +1,62 @@ +# Clearing the user's temporary folder +Get-ChildItem -Path "$env:TEMP" -File | Remove-Item -Force -EA SilentlyContinue + +# Clearing the Windows Temp folder +Remove-Item -Path 'C:\Windows\Temp\*' -Force -Recurse -EA SilentlyContinue + +# Exclude the AME folder while deleting directories in the temporary folder +Get-ChildItem -Path "$env:TEMP" -Directory | Where-Object { $_.Name -ne 'AME' } | Remove-Item -Force -Recurse -EA SilentlyContinue + +# As cleanmgr has multiple processes, there's no point in making the window hidden as it won't apply +function Invoke-AtlasDiskCleanup { + # Kill running cleanmgr instances, as they will prevent new cleanmgr from starting + Get-Process -Name cleanmgr -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue + # Disk Cleanup preset + # 2 = enabled + # 0 = disabled + $baseKey = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches' + $regValues = @{ + "Active Setup Temp Folders" = 2 + "BranchCache" = 2 + "D3D Shader Cache" = 0 + "Delivery Optimization Files" = 2 + "Diagnostic Data Viewer database files" = 2 + "Downloaded Program Files" = 2 + "Internet Cache Files" = 2 + "Language Pack" = 0 + "Old ChkDsk Files" = 0 + "Recycle Bin" = 0 + "RetailDemo Offline Content" = 2 + "Setup Log Files" = 2 + "System error memory dump files" = 2 + "System error minidump files" = 2 + "Temporary Files" = 0 + "Thumbnail Cache" = 2 + "Update Cleanup" = 2 + "User file versions" = 2 + "Windows Error Reporting Files" = 2 + "Windows Defender" = 2 + "Temporary Sync Files" = 2 + "Device Driver Packages" = 2 + } + foreach ($entry in $regValues.GetEnumerator()) { + $key = $entry.Key + $value = $entry.Value + $path = "$baseKey\$key" + Set-ItemProperty -Path $path -Name 'StateFlags0064' -Value $value -Type DWORD + } + # Run preset 64 (0-65535) + Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:64" +} + +# Check for other installations of Windows +# If so, don't cleanup as it will also cleanup other drives +$excludedDrive = "C" +$drives = Get-PSDrive -PSProvider 'FileSystem' | Where-Object { $_.Name -ne $excludedDrive } +foreach ($drive in $drives) { + if (Test-Path -Path $(Join-Path -Path $drive.Root -ChildPath 'Windows') -PathType Container) { + $otherInstalls = $true + } +} + +if (!($otherInstalls)) { Invoke-AtlasDiskCleanup } \ No newline at end of file diff --git a/src/Executables/PROMPTS.ps1 b/src/Executables/PROMPTS.ps1 index f2383f948a..73401780d5 100644 --- a/src/Executables/PROMPTS.ps1 +++ b/src/Executables/PROMPTS.ps1 @@ -90,80 +90,4 @@ if ($intButton -eq '7') { # if 'No' } else { Set-ItemProperty -Path $memIntegrity -Name "Enabled" -Value 1 -Type DWord Set-ItemProperty -Path $memIntegrity -Name "WasEnabledBy" -Value 2 -Type DWord -} - -<# - -------------------------- - Cleanmgr - -------------------------- -#> - -# As cleanmgr has multiple processes, there's no point in making the window hidden as it won't apply -function Invoke-AtlasDiskCleanup { - # Kill running cleanmgr instances, as they will prevent new cleanmgr from starting - Get-Process -Name cleanmgr | Stop-Process -Force - # Cleanmgr preset - # 2 = enabled - # 0 = disabled - $baseKey = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches' - $regValues = @{ - "Active Setup Temp Folders" = 2 - "BranchCache" = 2 - "D3D Shader Cache" = 2 - "Delivery Optimization Files" = 2 - "Diagnostic Data Viewer database files" = 2 - "Downloaded Program Files" = 2 - "Internet Cache Files" = 2 - "Language Pack" = 0 - "Old ChkDsk Files" = 0 - "Recycle Bin" = 0 - "RetailDemo Offline Content" = 2 - "Setup Log Files" = 2 - "System error memory dump files" = 2 - "System error minidump files" = 2 - "Temporary Files" = 0 - "Thumbnail Cache" = 2 - "Update Cleanup" = 2 - "User file versions" = 2 - "Windows Error Reporting Files" = 2 - } - foreach ($entry in $regValues.GetEnumerator()) { - $key = $entry.Key - $value = $entry.Value - $path = "$baseKey\$key" - Set-ItemProperty -Path $path -Name 'StateFlags0064' -Value $value -Type DWORD - } - # Run preset 64 (0-65535) - Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:64" -} - -# Check for other installations of Windows -# If so, show the prompt, if not, run Disk Cleanup without input -$excludedDrive = "C" -$drives = Get-PSDrive -PSProvider 'FileSystem' | Where-Object { $_.Name -ne $excludedDrive } -foreach ($drive in $drives) { - if (Test-Path -Path $(Join-Path -Path $drive.Root -ChildPath 'Windows') -PathType Container) { - $otherInstalls = $true - } -} - -$WindowTitle = 'Disk Cleanup - Atlas' - -$Message = @' -Would you like to run Disk Cleanup (with the Atlas preset)? - -Disk Cleanup is a built-in tool in Windows for freeing disk space by removing temporary files, which is good (in this case) to have a clean base installation. - -Due to a Disk Cleanup limitation in Windows, you can only clean all drives on a system when using a Disk Cleanup preset, not just the current installation. - -Although nothing unexpected should come from using Disk Cleanup, this will modify other installations of Windows on your computer. - -Automatically selecting 'No' in 5 minutes... -'@ - -if ($otherInstalls) { - # Default option is 'No' - $intButton = '7' - $intButton = $sh.Popup($Message,300,$WindowTitle,4+48+256) - if ($intButton -eq '6') {Invoke-AtlasDiskCleanup} -} else {Invoke-AtlasDiskCleanup} \ No newline at end of file +} \ No newline at end of file