Skip to main content

Run Disk Cleanup - Windows

Summary

This is a CW RMM task that will run the Cleanmgr included in Windows. It will set all optional Cleanmgr targets to enabled, except for the Downloads folder. The script will output both before and after available disk space when the tool was run.

Sample Run

Image

Select Run Now and click Run Task

Image

Task Creation

Script Details

Step 1

Navigate to AutomationTasks
step1

Step 2

Create a new Script Editor style task by choosing the Script Editor option from the Add dropdown menu
step2

The New Script page will appear on clicking the Script Editor button:
step3

Step 3

Fill in the following details in the Description section:

  • Name: Run Disk Cleanup - Windows
  • Description: Runs the Cleanmgr included in Windows. It will set all optional Cleanmgr targets to enabled except for the Downloads folder.
  • Category: Maintenance

Image

Script Editor

Click the Add Row button in the Script Editor section to start creating the script
AddRow

A blank function will appear:
BlankFunction

Row 1 Function: PowerShell script

Search and select the PowerShell script function.

Image

Paste in the following PowerShell script and set the Expected Time of Script Execution to 300 seconds.

$osDrive = Get-CimInstance -Class Win32_OperatingSystem | Select-Object -Property SystemDrive
$osDriveSize = Get-CimInstance -Class Win32_LogicalDisk -filter "DeviceID='$($osDrive.SystemDrive)'" | Select-Object @{Name = "SizeGB"; Expression = { $_.Size / 1GB -as [float] } }
$freeSpaceGB = (Get-CimInstance -Class Win32_LogicalDisk -Filter "DeviceID='$($osDrive.SystemDrive)'" | Select-Object @{Name = "FreeSpaceGB"; Expression = { $_.FreeSpace / 1GB -as [float] } }).FreeSpaceGB

if(Test-Path -Path "$env:SystemRoot\System32\cleanmgr.exe") {
Get-Process cleanmgr -ErrorAction SilentlyContinue | Stop-Process -PassThru

$volumeCaches = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
foreach($key in $volumeCaches)
{
if ($key -like "*DownloadsFolder") { continue }
New-ItemProperty -Path "$($key.PSPath)" -Name StateFlags1619 -Value 2 -Type DWORD -Force | Out-Null
}

Start-Process -Wait "$env:SystemRoot\System32\cleanmgr.exe" -ArgumentList "/sagerun:1619"
}

$newFreeSpaceGB = (Get-CimInstance -Class Win32_LogicalDisk -Filter "DeviceID='$($osDrive.SystemDrive)'" | Select-Object @{Name = "FreeSpaceGB"; Expression = { $_.FreeSpace / 1GB -as [float] } }).FreeSpaceGB

return "Previous Drive Space Available = $freeSpaceGB GB`nCurrent Drive Space Available = $newFreeSpaceGB GB"

Image

Save and move to the next row

Row 2 Function: Script Log

Search and select the Script Log function.

Image

In the script log message, simply type %output% so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Image

Save Task

Click the Save button at the top-right corner of the screen to save the script.
SaveButton

Completed Task

Image

Output

  • Script Log