Skip to content

Commit

Permalink
feat: cleanup script (replace prompt)
Browse files Browse the repository at this point in the history
  • Loading branch information
he3als committed Jul 9, 2023
1 parent c85600a commit 5be99ed
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/Configuration/tweaks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 0 additions & 18 deletions src/Configuration/tweaks/debloat/cleanup-temp-files.yml

This file was deleted.

10 changes: 10 additions & 0 deletions src/Configuration/tweaks/scripts/script-cleanup.yml
Original file line number Diff line number Diff line change
@@ -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
62 changes: 62 additions & 0 deletions src/Executables/CLEANUP.ps1
Original file line number Diff line number Diff line change
@@ -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 }
78 changes: 1 addition & 77 deletions src/Executables/PROMPTS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

0 comments on commit 5be99ed

Please sign in to comment.