<# .TITLE Disk Cleanup Detection Script .SYNOPSIS Detects if system requires disk cleanup based on temp file accumulation .DESCRIPTION Checks Windows temp folders and recycle bin size. Returns exit code 1 if more than 1GB can be cleaned up. .TAGS Remediation,Detection .REMEDIATIONTYPE Detection .PAIRSCRIPT remediate-disk-cleanup.ps1 .PLATFORM Windows .MINROLE Intune Service Administrator .PERMISSIONS DeviceManagementManagedDevices.ReadWrite.All .AUTHOR Ugur Koc .VERSION 1.1 .CHANGELOG 1.1 - Fixed invalid return statement in Get-FolderSize that caused folder sizes to always report 0 bytes 1.0 - Initial version .LASTUPDATE 2026-07-19 .EXAMPLE .\detect-disk-cleanup-needed.ps1 .NOTES Runs in SYSTEM context #> $ErrorActionPreference = "Stop" $threshold = 1GB function Get-FolderSize { param([string]$Path) if (Test-Path $Path) { try { $size = (Get-ChildItem -Path $Path -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum if ($null -eq $size) { return 0 } return $size } catch { return 0 } } return 0 } try { $totalSize = 0 # Windows Temp $totalSize += Get-FolderSize "$env:WINDIR\Temp" # User Temp folders Get-ChildItem "C:\Users" -Directory -ErrorAction SilentlyContinue | ForEach-Object { $totalSize += Get-FolderSize "$($_.FullName)\AppData\Local\Temp" } # Recycle Bin try { $shell = New-Object -ComObject Shell.Application $shell.NameSpace(0xA).Items() | ForEach-Object { $totalSize += $_.ExtendedProperty("Size") } } catch { # Silently continue if unable to access recycle bin } Write-Output "Cleanable space: $([math]::Round($totalSize / 1GB, 2)) GB" if ($totalSize -gt $threshold) { exit 1 } exit 0 } catch { Write-Error $_ exit 2 } # Copia sin cambios de https://github.com/ugurkocde/IntuneAutomation (commit 797bb2e). # MIT License # # Copyright (c) 2025 Ugur Koc | Microsoft MVP # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE.