Add-Type -AssemblyName System.Windows.Forms
$form = New-Object Windows.Forms.Form
$form.Text = "Drive Cleanup Tool"
$form.Size = New-Object System.Drawing.Size(300,150)
$form.StartPosition = "CenterScreen"
$label = New-Object Windows.Forms.Label
$label.Text = "Choose a drive to clean:"
$label.AutoSize = $true
$label.Location = New-Object System.Drawing.Point(90,20)
$form.Controls.Add($label)
function Launch-Cleanup {
param($letter)
$drive = "${letter}:"
try {
$shell = New-Object -ComObject 'Shell.Application'
$folder = $shell.Namespace(17)
$item = $folder.ParseName($drive)
$item.InvokeVerb('Properties')
Start-Sleep -Milliseconds 800
} catch {
[System.Windows.Forms.MessageBox]::Show("Failed to open properties for $drive.`n$($_.Exception.Message)", "Error")
}
try {
Start-Process -FilePath (Join-Path ([Environment]::SystemDirectory) 'cleanmgr.exe') `
-ArgumentList @("/d", $letter.ToUpper()) -Wait
} catch {
[System.Windows.Forms.MessageBox]::Show("Disk Cleanup failed for $drive.`n$($_.Exception.Message)", "Error")
}
}
$cButton = New-Object Windows.Forms.Button
$cButton.Text = "Clean C:"
$cButton.Size = New-Object System.Drawing.Size(80,30)
$cButton.Location = New-Object System.Drawing.Point(50,60)
$cButton.Add_Click({ Launch-Cleanup 'C'; $form.Close() })
$form.Controls.Add($cButton)
$dButton = New-Object Windows.Forms.Button
$dButton.Text = "Clean D:"
$dButton.Size = New-Object System.Drawing.Size(80,30)
$dButton.Location = New-Object System.Drawing.Point(160,60)
$dButton.Add_Click({ Launch-Cleanup 'D'; $form.Close() })
$form.Controls.Add($dButton)
[void]$form.ShowDialog()