Forum Discussion

dilanmic's avatar
dilanmic
Iron Contributor
Nov 12, 2024

Monitor low disk space for computers

Hi All,

 

We have a requirement to monitor low disk space, particularly on devices with less than 1GB of available space. We were considering creating a custom compliance policy, but this would lead to blocking access to company resources as soon as the device becomes non-compliant. Therefore, we were wondering if there are any other automated methods we could use to monitor the logical disk space (primarily the C drive) using Intune or Microsoft Graph.

 

Thanks in advance,

Dilan

  • Hi,

    You could us a pro active remediation script that triggers on disk space that fall below 1GB. See more info here: https://learn.microsoft.com/en-us/mem/intune/fundamentals/remediations

     

    As example a detection script for this scenario I asked ChatGPT to create (purely as example and needs to be checked and refined further):

     

    $ErrorActionPreference = "Stop"
    
    # Set the threshold (1 GB)
    $thresholdGB = 1
    $thresholdBytes = $thresholdGB * 1GB
    
    # Get the free space on the C: drive (you can change this if needed)
    $drive = Get-PSDrive -Name C
    
    # Check if free space is below threshold
    if ($drive.Free -lt $thresholdBytes) {
        # Return a failure exit code for Intune remediation
        Write-Host "Disk space is below 1 GB. Triggering remediation."
        exit 1
    } else {
        # Disk space is sufficient
        Write-Host "Disk space is above the 1 GB threshold."
        exit 0
    }

     

    You can trigger remediation script to perform something, not sure what you like to do but that should be a second script.

     

    Hope this helps.

     

    Regards,

     

    Sebastiaan

  • Beredan's avatar
    Beredan
    Copper Contributor

    I agree with the intent of using Remediation instead to detect and clean up.

    Here is an example of what I have recently setup. Use at your own risk!

    Detection script for less than 30GB

    $MinimumThreshold = 30GB
    $FreeSpace = (Get-Volume -DriveLetter ($env:SystemDrive -replace ":")).SizeRemaining
    
    if($FreeSpace -lt $MinimumThreshold){
        exit 1
    }else{
        exit 0
    }

    Remediation script to clear Configuration Manager Cache, run Disk Cleanup and clean up temp directories.

    ## 1. Clean out Configuration Manager Cache Directory ##
    [__comobject]$CCMComObject = New-Object -ComObject 'UIResource.UIResourceMgr'
    $CacheInfo = $CCMComObject.GetCacheInfo().GetCacheElements()
    ForEach ($CacheItem in $CacheInfo) {
        $null = $CCMComObject.GetCacheInfo().DeleteCacheElement([string]$($CacheItem.CacheElementID))
    }
    
    ## 2. Run Disk Clean-up with selection actions ##
    $CleanupSelection = @('Temporary Files','System error minidump files','System error memory dump files','Recycle Bin','Device Driver Packages','Windows Error Reporting Files')
    Foreach ($KeyName in $CleanupSelection) {
        $NewItemParams = @{
            Path         = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$KeyName"
            Name         = 'StateFlags0001'
            Value        = 1
            PropertyType = 'DWord'
            ErrorAction  = 'SilentlyContinue'
        }
        New-ItemProperty @NewItemParams -Force | Out-Null
    }
    $CleanMgr = Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -NoNewWindow -PassThru
    $CleanMgr | Wait-Process -Timeout 300 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    
    ## 3. Clean out Temp Directory and Recycling Manually ##
    Remove-Item -path "$Env:Windir\TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    Remove-Item -path "$Env:SystemDrive\`$Recycle.Bin" -Recurse -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue

     

Resources