Use Endpoint Analytics to clean up the disk

I have already written several blog posts about endpoint analytics. In the Microsoft Tech Community the question came up how to clean up the disk using Intune. This is a question that is difficult to answer generically as it is always very specific. Through more and more applications and data moving to the cloud and the storage is also becoming cheaper and cheaper, the amount of storage needed on a workplace devices and the problems with full hard disks are no longer as present as in the past.

In this blog I will show you how to free up disk space on your clients with an high disk usage. So let’s get started.

How to clean up storage

As mentioned above, it is difficult to give a generic answer here. It always depends on why the disk is full, are there large applications installed on the computer, is there a lot of caching on the computer or does the user simply have many files. We do not want to delete something that the user needs. Of course there is the possibility to delete a few temp data, but even these often have their purpose. You can e.g. clear the delivery optimization cache but if you do this on all clients then of course delivery optimization will not be as efficient. There are several articles that give tips on what you can do to free up space on a system.

Update 18.07.2022: With storage sense, Microsoft also offers a possibility to clean up the disk. This is an integrated mechanism that is built in with windows. It will for example move unused data as online data to OneDrive or delete temp files. Storage sense can also be configured via Intune.

So storage sense is a very good way to clean up the disk on a scheduled based or during little diskspace.

But storage sense has a disadvantage, you can not granularly configure what should be deleted, so you have here less custom configuration options and you don’t have an central monitoring. This method does also not allow you to delete custom files from e.g. applications. So let’s see what else windows has to offer.

There is also an old but good tool build in Windows to cleanup the disk. The CleanMgr.exe or Disk Cleanup is a possibility that exists for a long time in windows to clean up the disk. I have built the remediation script based on this tool.

How does the script work

First I have a very simple detection script which only checks if 15gb or less are still free. If this is the case the detection is fulfilled if not then not. Only if the detection is fulfilled the remediation is executed.

<#
Version: 1.0
Author: Jannik Reinhard (jannikreinhard.com)
Script: Get-CleanUpDiskDetection
Description:
Cleanup disk when utilization <15GB
Release notes:
Version 1.0: Init
#> 
$storageThreshold = 15

$utilization = (Get-PSDrive | Where {$_.name -eq "C"}).free

if(($storageThreshold *1GB) -lt $utilization){exit 0}
else{exit 1}

That was the simple task now we have to react when the free storage is less than 15 GB that we clean up the disk. To do this we need a remediation script. In the script you must first specify what you want to delete on the systems. These are the possible choices:

  • ‘Active Setup Temp Folders’
  • ‘BranchCache’
  • ‘Content Indexer Cleaner’
  • ‘Device Driver Packages’
  • ‘Downloaded Program Files’
  • ‘GameNewsFiles’
  • ‘GameStatisticsFiles’
  • ‘GameUpdateFiles’
  • ‘Internet Cache Files’
  • ‘Memory Dump Files’
  • ‘Offline Pages Files’
  • ‘Old ChkDsk Files’
  • ‘Previous Installations’
  • ‘Recycle Bin’
  • ‘Service Pack Cleanup’
  • ‘Setup Log Files’
  • ‘System error memory dump files’
  • ‘System error minidump files’
  • ‘Temporary Files’
  • ‘Temporary Setup Files’
  • ‘Temporary Sync Files’
  • ‘Thumbnail Cache’
  • ‘Update Cleanup’
  • ‘Upgrade Discarded Files’
  • ‘User file versions’
  • ‘Windows Defender’
  • ‘Windows Error Reporting Archive Files’
  • ‘Windows Error Reporting Queue Files’
  • ‘Windows Error Reporting System Archive Files’
  • ‘Windows Error Reporting System Queue Files’
  • ‘Windows ESD installation files’
  • ‘Windows Upgrade Log Files’

Select from this list what you want to clean up and write this into the variable $cleanupTypeSelection. The script how to free up the disk via the CleanMgr.exe can be found here or on my Git hub repository

You can also customize the script to delete custom files or folders from e.g. specific applications. Rudy Ooms shared a nice PowerShell to delete some more local files.

<#
Version: 1.0
Author: Jannik Reinhard (jannikreinhard.com)
Script: Get-CleanUpDiskRemediation
Description:
Cleanup disk when utilization <15GB
Release notes:
Version 1.0: Init
#> 

$cleanupTypeSelection = 'Temporary Sync Files', 'Downloaded Program Files', 'Memory Dump Files', 'Recycle Bin'

foreach ($keyName in $cleanupTypeSelection) {
    $newItemParams = @{
        Path         = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$keyName"
        Name         = 'StateFlags0001'
        Value        = 1
        PropertyType = 'DWord'
        ErrorAction  = 'SilentlyContinue'
    }
    New-ItemProperty @newItemParams | Out-Null
}

Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -NoNewWindow -Wait

Create remediation script

  • Enter a name and click Next
  • Upload the Detection and the Remediation script
  • Click Next > Next
  • Assign the script to a group
  • Create a schedule. In my case I select daily.
  • Click Next > Create

Conclusion

In this blog I described how you can use Endpoint analytics to clean up disks that are almost full. You can configure the script as you need it. You can of course add other cleanup activities in addition to the CleanMgr.exe and delete custom files. You can also build a monitoring based on the state of the script (Recurred) and identify devices where the cleanup script did not help and replace them if necessary, provide them with more storage or delete some unused applications.

However, it is important to mention that Storage sense is the new method that Microsoft is offer and will continue to develop. Disk cleanup is still a part of Windows but will not be updated with new features.

I hope I could help you with my blog, so that you can react directly in your environment when the storage is full.

Stay healthy, Cheers
Jannik

2 thoughts on “Use Endpoint Analytics to clean up the disk

Comments are closed.