From 4440f1cd688ebf2e1441906eac07629b4999d3f1 Mon Sep 17 00:00:00 2001 From: P6g9YHK6 <17877371+P6g9YHK6@users.noreply.github.com> Date: Wed, 6 Aug 2025 10:10:40 +0200 Subject: [PATCH 1/6] Update file: Updater P3 Run Cleaner.ps1 --- scripts_staging/TasksUpdater/Updater P3 Run Cleaner.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts_staging/TasksUpdater/Updater P3 Run Cleaner.ps1 b/scripts_staging/TasksUpdater/Updater P3 Run Cleaner.ps1 index 762e51cd..9c4a7765 100644 --- a/scripts_staging/TasksUpdater/Updater P3 Run Cleaner.ps1 +++ b/scripts_staging/TasksUpdater/Updater P3 Run Cleaner.ps1 @@ -32,9 +32,11 @@ .CHANGELOG 28.11.24 SAN Incorporated VHDX cleaner. 13.12.24 SAN Split logging from parser. -#> + 06.08.25 SAN upgrade to ps7 +#> +{{CallPowerShell7}} # Name will be used for both the name of the log file and what line of the Schedules to parse $PartName = "TempFileCleanup" From b9247811e276ce47abcc8c44b4a489fab8e6d730 Mon Sep 17 00:00:00 2001 From: P6g9YHK6 <17877371+P6g9YHK6@users.noreply.github.com> Date: Wed, 6 Aug 2025 13:10:52 +0200 Subject: [PATCH 2/6] Update file: Updater P3 Run Cleaner.ps1 --- scripts_staging/TasksUpdater/Updater P3 Run Cleaner.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts_staging/TasksUpdater/Updater P3 Run Cleaner.ps1 b/scripts_staging/TasksUpdater/Updater P3 Run Cleaner.ps1 index 9c4a7765..72ed00af 100644 --- a/scripts_staging/TasksUpdater/Updater P3 Run Cleaner.ps1 +++ b/scripts_staging/TasksUpdater/Updater P3 Run Cleaner.ps1 @@ -26,6 +26,7 @@ Logging snippet for logging Updater P3.5 Schedules parser snippet for parsing the date Cleaner & VHDXCleaner snippet to run the actual cleans + Ps7 snippnet #public From d0f34c9082bde9b4d431b7bb91ddf49740e86e3e Mon Sep 17 00:00:00 2001 From: P6g9YHK6 <17877371+P6g9YHK6@users.noreply.github.com> Date: Mon, 11 Aug 2025 10:54:20 +0200 Subject: [PATCH 3/6] Update file: Cleaner.ps1 --- scripts_staging/snippets/Cleaner.ps1 | 54 +++++----------------------- 1 file changed, 9 insertions(+), 45 deletions(-) diff --git a/scripts_staging/snippets/Cleaner.ps1 b/scripts_staging/snippets/Cleaner.ps1 index f3c6bd6e..434f4713 100644 --- a/scripts_staging/snippets/Cleaner.ps1 +++ b/scripts_staging/snippets/Cleaner.ps1 @@ -24,6 +24,7 @@ 19.11.24 SAN added cleanup of search index 17.12.24 SAN Full code refactoring, set a single value for file expiration 14.01.25 SAN More verbose output for the deletion of items + 11.08.25 SAN Run cleanmgr in the background .TODO Integrate bleachbit this would help avoid having to update this script too often. @@ -41,50 +42,7 @@ $Starters = Get-Date # Function to retrieve and display disk space info function Get-DiskInfo { - $DiskInfo = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 } | - Select-Object SystemName, - @{ Name = "Drive"; Expression = { $_.DeviceID } }, - @{ Name = "Size (GB)"; Expression = { "{0:N1}" -f ($_.Size / 1GB) } }, - @{ Name = "FreeSpace (GB)"; Expression = { "{0:N1}" -f ($_.FreeSpace / 1GB) } }, - @{ Name = "PercentFree"; Expression = { "{0:P1}" -f ($_.FreeSpace / $_.Size) } } - return $DiskInfo -} - -function Remove-Items { - param ( - [string]$Path, - [int]$Days - ) - - if (Test-Path $Path) { - # Check if the Path is a file - if ((Get-Item $Path).PSIsContainer -eq $false) { - try { - # Remove the single file if it meets the age condition - if ((Get-Item $Path).CreationTime -lt (Get-Date).AddDays(-$Days)) { - Remove-Item -Path $Path -Force -Verbose -Confirm:$false - Write-Host "[DONE] Removed single item: $Path" - } else { - Write-Host "[INFO] $Path does not meet the age condition, skipping removal." - } - } catch { - Write-Host "[ERROR] Failed to remove item: $Path. $_" - } - } else { - try { - # Get all items in the folder - $items = Get-ChildItem -Path $Path -Recurse -Force | - Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-$Days) } | - Sort-Object { $_.Name.Length } -Descending - - if ($items.Count -gt 0) { - Write-Host "[INFO] Listing items for removal in order of name length:" - foreach ($item in $items) { - Write-Host " - $($item.FullName)" - } - - # Remove items from longest name to shortest - $items | Remove-Item -Force -Recurse -Verbose -Confirm:$false + $DiskInfo = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveTypCleaner Write-Host "[DONE] Cleaned up directory: $Path" } else { Write-Host "[INFO] No items met the age condition in directory: $Path" @@ -207,7 +165,13 @@ foreach ($Path in $UserPathsToClean.Values) { Add-RegistryKeys-CleanMGR # Run Disk Cleanup with custom settings -Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:1" -Wait +$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo +$processStartInfo.FileName = "cleanmgr.exe" +$processStartInfo.Arguments = "/sagerun:1" +$processStartInfo.UseShellExecute = $true # Allows the executable to run independently +$processStartInfo.CreateNoWindow = $true # Prevents a new window from being created +# Start the process (no wait) +[System.Diagnostics.Process]::Start($processStartInfo) | Out-Null # Gather disk usage after cleanup $After = Get-DiskInfo | Format-Table -AutoSize | Out-String From ee77fb4cdcc8ccbb64dbd9630de9791e3ac80956 Mon Sep 17 00:00:00 2001 From: P6g9YHK6 <17877371+P6g9YHK6@users.noreply.github.com> Date: Mon, 18 Aug 2025 17:15:22 +0200 Subject: [PATCH 4/6] Update file: Cleaner.ps1 --- scripts_staging/snippets/Cleaner.ps1 | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/scripts_staging/snippets/Cleaner.ps1 b/scripts_staging/snippets/Cleaner.ps1 index 434f4713..fb91b0a5 100644 --- a/scripts_staging/snippets/Cleaner.ps1 +++ b/scripts_staging/snippets/Cleaner.ps1 @@ -25,6 +25,7 @@ 17.12.24 SAN Full code refactoring, set a single value for file expiration 14.01.25 SAN More verbose output for the deletion of items 11.08.25 SAN Run cleanmgr in the background + 18.18.25 SAN fix disk info error .TODO Integrate bleachbit this would help avoid having to update this script too often. @@ -42,21 +43,24 @@ $Starters = Get-Date # Function to retrieve and display disk space info function Get-DiskInfo { - $DiskInfo = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveTypCleaner - Write-Host "[DONE] Cleaned up directory: $Path" - } else { - Write-Host "[INFO] No items met the age condition in directory: $Path" - } - } catch { - Write-Host "[ERROR] Failed to clean up directory: $Path. $_" + try { + $DiskInfo = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 } # 3 = Local Disk + foreach ($disk in $DiskInfo) { + [PSCustomObject]@{ + DeviceID = $disk.DeviceID + VolumeName = $disk.VolumeName + FileSystem = $disk.FileSystem + FreeSpaceGB = [math]::Round($disk.FreeSpace / 1GB, 2) + SizeGB = [math]::Round($disk.Size / 1GB, 2) } } - } else { - Write-Host "[WARNING] $Path does not exist, skipping cleanup." + } catch { + Write-Host "[ERROR] Failed to retrieve disk information. $_" } } + # Function to add or update registry keys for Disk Cleanup function Add-RegistryKeys-CleanMGR { $baseKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches" From 8e14093cfad9302523cfe47aaa486351a9b27157 Mon Sep 17 00:00:00 2001 From: P6g9YHK6 <17877371+P6g9YHK6@users.noreply.github.com> Date: Mon, 18 Aug 2025 17:21:55 +0200 Subject: [PATCH 5/6] Update file: Cleaner.ps1 --- scripts_staging/snippets/Cleaner.ps1 | 33 ++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/scripts_staging/snippets/Cleaner.ps1 b/scripts_staging/snippets/Cleaner.ps1 index fb91b0a5..5f136702 100644 --- a/scripts_staging/snippets/Cleaner.ps1 +++ b/scripts_staging/snippets/Cleaner.ps1 @@ -25,7 +25,7 @@ 17.12.24 SAN Full code refactoring, set a single value for file expiration 14.01.25 SAN More verbose output for the deletion of items 11.08.25 SAN Run cleanmgr in the background - 18.18.25 SAN fix disk info error + 18.18.25 SAN fix disk info error, missing function, sccm condition and made it move verbose. .TODO Integrate bleachbit this would help avoid having to update this script too often. @@ -37,8 +37,7 @@ $DaysToDelete = if ([string]::IsNullOrEmpty($env:DaysToDelete)) { 30 } else { [i Write-Host "Days to delete set to: $DaysToDelete" -$VerbosePreference = "Continue" -$ErrorActionPreference = "SilentlyContinue" + $Starters = Get-Date # Function to retrieve and display disk space info @@ -59,7 +58,17 @@ function Get-DiskInfo { } } - +function Remove-Items { + param( + [string]$Path, + [int]$Days + ) + if (Test-Path $Path) { + Get-ChildItem -Path $Path -Recurse -Force -ErrorAction SilentlyContinue | + Where-Object { -not $_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-$Days) } | + Remove-Item -Force -Recurse -ErrorAction SilentlyContinue + } +} # Function to add or update registry keys for Disk Cleanup function Add-RegistryKeys-CleanMGR { @@ -119,13 +128,19 @@ $Before = Get-DiskInfo | Format-Table -AutoSize | Out-String Stop-Service -Name wuauserv -Force -ErrorAction SilentlyContinue -Verbose # Adjust SCCM cache size if configured -$cache = Get-WmiObject -Namespace root\ccm\SoftMgmtAgent -Class CacheConfig -if ($cache) { - $cache.size = 1024 - $cache.Put() | Out-Null - Restart-Service ccmexec -ErrorAction SilentlyContinue +try { + $cache = Get-WmiObject -Namespace root\ccm\SoftMgmtAgent -Class CacheConfig -ErrorAction Stop + if ($cache) { + $cache.size = 1024 + $cache.Put() | Out-Null + Restart-Service ccmexec -ErrorAction SilentlyContinue + Write-Host "[INFO] SCCM cache size adjusted and ccmexec service restarted." + } +} catch { + Write-Host "[INFO] SCCM client not detected. Skipping cache configuration." } + # Compaction of Windows.edb $windowsEdbPath = "$env:ALLUSERSPROFILE\\Microsoft\\Search\\Data\\Applications\\Windows\\Windows.edb" if (Test-Path $windowsEdbPath) { From e2d22b1793c02974028e7be6d6b570ba7d411aa6 Mon Sep 17 00:00:00 2001 From: P6g9YHK6 <17877371+P6g9YHK6@users.noreply.github.com> Date: Wed, 10 Sep 2025 09:17:30 +0000 Subject: [PATCH 6/6] Update file: Cleaner.ps1 --- scripts_staging/snippets/Cleaner.ps1 | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scripts_staging/snippets/Cleaner.ps1 b/scripts_staging/snippets/Cleaner.ps1 index 5f136702..f6e47e82 100644 --- a/scripts_staging/snippets/Cleaner.ps1 +++ b/scripts_staging/snippets/Cleaner.ps1 @@ -25,7 +25,8 @@ 17.12.24 SAN Full code refactoring, set a single value for file expiration 14.01.25 SAN More verbose output for the deletion of items 11.08.25 SAN Run cleanmgr in the background - 18.18.25 SAN fix disk info error, missing function, sccm condition and made it move verbose. + 18.08.25 SAN fix disk info error, missing function, sccm condition and made it move verbose. + 10.09.25 SAN added output for to check if cleanmgr is running .TODO Integrate bleachbit this would help avoid having to update this script too often. @@ -182,7 +183,6 @@ foreach ($Path in $UserPathsToClean.Values) { # Add registry keys for Disk Cleanup Add-RegistryKeys-CleanMGR - # Run Disk Cleanup with custom settings $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo $processStartInfo.FileName = "cleanmgr.exe" @@ -191,6 +191,14 @@ $processStartInfo.UseShellExecute = $true # Allows the executable to run indepe $processStartInfo.CreateNoWindow = $true # Prevents a new window from being created # Start the process (no wait) [System.Diagnostics.Process]::Start($processStartInfo) | Out-Null +Start-Sleep -Seconds 5 +$process = Get-Process -Name "cleanmgr" -ErrorAction SilentlyContinue +if ($null -ne $process) { + Write-Output "[DONE] Disk Cleanup is running." +} else { + Write-Output "[ERR] Disk Cleanup is NOT running." +} + # Gather disk usage after cleanup $After = Get-DiskInfo | Format-Table -AutoSize | Out-String