This PowerShell script helps maintain system performance by identifying large files and folders on specified drives. It not only lists these files but also offers an option to delete them, making it an effective tool for disk space management.
powershell
# Define the path to search for large files
$searchPath = "C:\"
# Set the minimum file size threshold in MB
$sizeThresholdMB = 100
$sizeThresholdBytes = $sizeThresholdMB * 1MB
# Get files larger than the threshold in the specified path
$largeFiles = Get-ChildItem -Path $searchPath -Recurse -File | Where-Object { $_.Length -gt $sizeThresholdBytes }
# Display file details: Name, Size (MB), and Path
$largeFiles | Select-Object Name, @{Name="Size (MB)"; Expression={[math]::round($_.Length / 1MB, 2)}}, FullName | Format-Table -AutoSize
# Ask user for deletion
$deleteFiles = Read-Host "Do you want to delete any of these files? (Y/N)"
if ($deleteFiles -eq 'Y') {
foreach ($file in $largeFiles) {
# Prompt for each file deletion
$confirmDelete = Read-Host "Do you want to delete $($file.FullName)? (Y/N)"
if ($confirmDelete -eq 'Y') {
Remove-Item -Path $file.FullName -Force
Write-Host "Deleted: $($file.FullName)"
}
}
} else {
Write-Host "No files will be deleted."
}
Write-Host "Disk space cleanup operation completed."Run it across your fleet
This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.
More in Monitoring & Health
User Account Status Reporting Script
2025-01-13Automated Disk Space Monitoring Script
2025-01-13Monitor System Performance with PowerShell
2025-01-12Automating System Health Check with PowerShell
2025-01-05Automate System Performance Monitoring with PowerShell
2024-12-30Enhancing System Monitoring with PowerShell: Real-time Log Management
2024-12-29Ready when you are.
Try ServerEngine free for 7 days.