Skip to content

Commit

Permalink
Refactor CLEANER.ps1 script
Browse files Browse the repository at this point in the history
  • Loading branch information
melo936 authored Dec 9, 2023
1 parent 582eaa9 commit c593b83
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Executables/CLEANER.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$ErrorActionPreference = 'SilentlyContinue'

Write-Host "Using Disk Cleanup with custom configuration"
$volumeCache = @{
Expand Down Expand Up @@ -31,15 +32,15 @@ $registryPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Volume
foreach ($item in $volumeCache.GetEnumerator()) {
$keyPath = Join-Path $registryPath $item.Key
if (Test-Path $keyPath) {
Write-Host "Cleaning up $item.Key"

New-ItemProperty -Path $keyPath -Name StateFlags1337 -Value $item.Value -PropertyType DWord | Out-Null
}
}

Start-Process -FilePath "$env:SystemRoot\system32\cleanmgr.exe" -ArgumentList "/sagerun:1337" -Wait:$false

Write-Host "Cleaning up Event Logs"
Get-EventLog -LogName * | ForEach { Clear-EventLog $_.Log }
Get-EventLog -LogName * | ForEach-Object { Clear-EventLog $_.Log }

Write-Host "Disabling Reserved Storage"
Set-WindowsReservedStorageState -State Disabled
Expand All @@ -66,7 +67,7 @@ $foldersToRemove = @(
foreach ($folderName in $foldersToRemove) {
$folderPath = Join-Path $env:SystemRoot $folderName
if (Test-Path $folderPath) {
Remove-Item -Path "$folderPath\*" -Force -Recurse -ErrorAction SilentlyContinue | Out-Null
Remove-Item -Path "$folderPath\*" -Force -Recurse | Out-Null
}
}

Expand All @@ -75,7 +76,7 @@ foreach ($folderName in $foldersToRemove) {
Get-ChildItem -Path "$env:SystemRoot" -Filter *.log -File -Recurse -Force | Remove-Item -Recurse -Force | Out-Null

Write-Host "Cleaning up %TEMP%"
Get-ChildItem -Path "$env:TEMP" -Exclude "AME" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "$env:TEMP" -Exclude "AME" | Remove-Item -Recurse -Force

# Just in case
Start-ScheduledTask -TaskPath "\Microsoft\Windows\DiskCleanup\" -TaskName "SilentCleanup"
Expand Down

0 comments on commit c593b83

Please sign in to comment.