Skip to content

Basic powershell cleanup script

Notifications You must be signed in to change notification settings

EZRunServiceDesk/Low-Disk-Space-Cleanup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸ’» PowerShell Disk Cleanup Script

This PowerShell script helps free up disk space and improve system performance on Windows by running various cleanup tasks. Each task is controlled by a toggle flag at the top of the script.


πŸ”§ Features

This script can perform the following cleanup actions:

  • πŸ—‘οΈ Temporary Files β€” Cleans TEMP folders (User and System)
  • ♻️ Recycle Bin β€” Empties the recycle bin
  • 🧹 Windows Update Cache β€” Removes downloaded update files
  • πŸͺŸ Windows.old β€” Deletes leftover files from old Windows installations
  • ⚑ Prefetch β€” Deletes prefetch data older than 30 days
  • 🚚 Delivery Optimization β€” Clears cached delivery optimization files
  • 🌿 BranchCache β€” Clears cached network files
  • πŸ’€ Hibernation File β€” Disables hibernation and removes hiberfil.sys
  • 🌐 DNS Cache β€” Flushes the DNS resolver cache
  • πŸ“‘ Log Files β€” Deletes IIS and system log files
  • πŸ—οΈ Component Store β€” Cleans up the WinSxS folder using DISM
  • πŸ‘€ Old User Profiles β€” Deletes user profiles not used for 180+ days
  • 🧰 Disk Cleanup Tool β€” Executes cleanmgr /sagerun:1
  • πŸ“Š Disk Report β€” Logs free and total space for each disk

🧾 Log Output

  • Log Folder: C:\temp\Disk_Space_Cleanup
  • Log Format: MM-dd-yyyy_Disk_Space_Cleanup.log
  • Each step logs a timestamped entry indicating:
    • Starting
    • Completed
    • Failed

βš™οΈ Configuration

The script is controlled by string flags at the top of the file:

$DoTemp = "True"
$DoWindowsUpdate = "True"
$DoWindowsOld = "True"
$DoRecycleBin = "True"
$DoPrefetch = "True"
$DoDeliveryOptimization = "True"
$DoBranchCache = "True"
$DoHibernation = "True"
$DoFlushDNS = "True"
$DoIISLogs = "True"
$DoComponentCleanup = "True"
$DoOldUserProfiles = "True"
$DoDiskReport = "True"
$DoDiskCleanupTool = "True"
Flag Description
$DoTemp Clean TEMP folders
$DoWindowsUpdate Clear Windows Update cache
$DoWindowsOld Remove Windows.old and reset component store
$DoRecycleBin Empty the recycle bin
$DoPrefetch Delete old prefetch files
$DoDeliveryOptimization Clear Delivery Optimization cache
$DoBranchCache Clear BranchCache
$DoHibernation Disable hibernation and remove hiberfil.sys
$DoFlushDNS Flush DNS resolver cache
$DoIISLogs Remove IIS and system logs
$DoComponentCleanup DISM component store cleanup
$DoOldUserProfiles Delete profiles not used in 180+ days
$DoDiskCleanupTool Run cleanmgr /sagerun:1
$DoDiskReport Log disk free/total space to the cleanup log

πŸ›‘οΈ Disclaimer This script is provided β€œas-is” without warranty. Please review and test in a safe environment before deploying in production.

Use at your own risk.