Automatic Disk Cleanup with Group Policy and SCCM
While Windows does an OK job of preventing low disk space, you can use Group Policy or SCCM to vastly improve those tasks and prevent low disk space problems on your clients.
In my environment, we had a few dozen machines with less than 10% free disk space. By using this setup, we now have zero and a few dozen less problems to worry about! When we purchase new computers, we are saving $100 per machine because we know that we can use a smaller SSD.
Setting up an automatic disk cleanup requires two components. First, you need a way to filter out machines that have low disk space from normal machines. Group Policy or SCCM can do this for you. Even though I personally use SCCM to do this, I’ll outline how to use both tools.
Second, you need a way to tighten the existing cleanup tasks while expanding the low disk cleanup to other common space hogs. SCCM can do this but Group Policy is my preferred solution for most of the settings.1
Finding Machines with Low Disk Space Using Group Policy
To find these computers with Group Policy, we can use a WMI Filter. If you have access to SCCM, I would recommend using SCCM to find computers instead – skip to the next heading.
If you do not have SCCM, Group Policy will work just fine. You won’t have access to reporting and processing will be a fraction slower. WMI filters do have to process every time and slow down Group Policy processing. This WMI filter is fast (.50ms on my test machine), so it is not a huge deal.
If you are using Group Policy, open the Group Policy Management Console and head down to the WMI Filters section. Create a new WMI filter and paste in the following query:
Select * from Win32_LogicalDisk where VolumeName = "OSDisk" AND FreeSpace < 10000000000
This query will equal TRUE on any computer when the OSDisk volume has less than 10GBs of free space. If you wish to apply the disk cleanup to all drives (including external/USB drives) in the machine, remove the VolumeName section. You can also adjust the FreeSpace size or reverse the less than symbol to a greater than symbol for testing.
If you do not have access SCCM, skip to the Cleaning Up Disk Space with Group Policy section – at the bottom of this article.
Find Machines with Low Disk Space Using SCCM
The Hardware Inventory in SCCM can be used to find computers with low disk space. From that, you can build out a Low Disk Space collection and target those devices with additional cleanup tasks.
First, make sure that your clients are reporting their Logical Disk Free Space in the hardware inventory. Under Administration/Client Settings, open your default client settings and select Hardware Inventory. Next, select Set Classes. Finally, expand Logical Disk (SMS_LogicalDisk) and ensure that Free Space (MB) is checked). If is not checked, check it and continue on – clients just won’t populate into your Low Disk Space collection until their next Hardware Inventory cycle.
Now, create a new Device Collection and add in the following query statement:
select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System inner join SMS_G_System_LOGICAL_DISK on SMS_G_System_LOGICAL_DISK.ResourceID = SMS_R_System.ResourceId where SMS_G_System_LOGICAL_DISK.FreeSpace <= 10000 and SMS_G_System_LOGICAL_DISK.VolumeName = "OSDisk"
Notice that is query is virtually the same as the Group Policy WMIFilter. The FreeSpace is measured in megabytes for SCCM instead of just bytes for Group Policy.
Cleaning Up Disk Space with SCCM
If you have SCCM, you can technically use it for all of the cleanup settings that are set using Group Policy in the section below. I find these settings easier and more reliable to implement with Group Policy.
For SCCM, we are only concerned with the client cache size. In the Configuration Manager console, create a new Client Device Settings. Under Client Cache Settings, set the Maximum cache size (either percentage of disk or MB) to a very low value.
Deploy these settings to just your Low Disk Space device collection.
Finally, create a new group in Active Directory that has the same name as your Low Disk Space device collection. Use these steps to sync your SCCM collection to that AD group. We will use this group to apply the Group Policy cleanup tasks.
Cleaning Up Disk Space with Group Policy
In the Group Policy Management Console, create a new GPO named something like “Cleanup Computers with Low Disk Space”.
If you are using the WMI filter to target your computers, leave the Security Filtering to Authenticated Users and set the WMI filter from None to your Low Disk filter.
If you are syncing a SCCM device collection to an AD security group, remove Authenticated Users and add your group to the Security Filtering section – don’t set a WMI filter.
Edit your GPO and add the following PowerShell shutdown script:
# Remove all items in these directories
Remove-Item -recurse -force "C:\Windows\Prefetch\*"
Remove-Item -recurse -force "C:\Windows\Temp\*"
Remove-Item -recurse -force "C:\Windows\Logs\CBS\*"
Remove-Item -recurse -force "C:\Windows\CSC\v2.0.6\namespace\*"
Remove-Item -recurse -force "C:\Users\*\AppData\Roaming\Microsoft\Windows\Recent Items\*"
# Remove items with exclusions
Remove-Item -recurse -force c:\Users\*\AppData\Local\* -exclude "Microsoft","Google"
Most of the files removed should be temp files. The exception is the \namespace\ line as it removes Offline files synced from a DFS namespace and the final line (exclusions line). When testing this, you may want to remove the Offline Files line and check the local user appdata folder for additional items to exclude.
Under Computer Configuration, set the following Administrative Templates – as pictured below:
In our environment, we had one final culprit that ate up disk space. Folder Redirection with Offline Files on shared desktops. To fix this, we can create a Registry Group Policy Preference to nuke the entire offline file cache. To do this, create a new computer side Registry preference:
- Hive: HKLM
- Key Path: SYSTEM\CurrentControlSet\Services\CSC\Parameters
- Value Name: FormatDatabase
- Value Type: Dword
- Value Data: 1
Most of these settings only apply on a shutdown so it might take a reboot or two to see them in action.
With all of this setup, you should no longer see computers with low disk space. This will prevent helpdesk calls and might even let you purchase smaller drives for your computers. If you have any suggestions or improvements, I would love to hear about them – just leave me a comment!