Saltar al contenido
Microsoft scripts

Scripts de Intune

Detectar equipos con más de 1 GB de basura limpiable

Mide el tamaño de las carpetas temporales de Windows y de cada usuario, más la Papelera. Si hay más de 1 GB dispara la limpieza.

Antes de correrloSolo mide. Si lo usas con remediate-disk-cleanup.ps1 revisa la advertencia de esa ficha antes de asignarlo. El permiso de Graph del encabezado no lo usa el código.

Ejemplo en México

Escuela de idiomas en Colima, 45 equipos de salones

Los equipos de salones se llenan de temporales y los discos pequeños ya marcan alerta de espacio.

detect-disk-cleanup-needed.ps1

Ve cuántos equipos pasan 1 GB de temporales y decide si activa la corrección.

Cómo implementarlo

  1. En el centro de administración de Microsoft Intune ve a Dispositivos > Administrar dispositivos > Scripts y correcciones (Scripts and remediations) > Remediations > Crear paquete de scripts.
  2. En Configuración sube detect-disk-cleanup-needed.ps1 como script de detección y remediate-disk-cleanup.ps1 como script de corrección.
  3. Deja desactivadas "Ejecutar con las credenciales del usuario" y "Aplicar verificación de firma", asígnalo primero a un grupo piloto y elige la frecuencia.
  4. Revisa en el paquete qué equipos pasaron el umbral de 1 GB.
Ver el código completo de detect-disk-cleanup-needed.ps1
<#
.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.

Relacionados