Automate file removal on windows using powershell and task scheduler

Managing disk space effectively is a perennial challenge in the digital world. For professionals and organizations dealing with large volumes of data, manual cleanup is not only time-consuming but also prone to errors. This is where PowerShell, Microsoft’s task automation framework, comes into play, offering a powerful solution for automating disk cleanup tasks to improve efficiency and accuracy.

Automating Cleanup with PowerShell


A prime example of this automation is the cleanDirectory.ps1 PowerShell script, designed to facilitate the automated removal of files and folders older than a specified number of days. Below is a simplified version of the script, which you can customize and use in your own environment:

# Usage: ./cleanDirectory.ps1 <Path> <AgeInDays>
# Example: ./cleanDirectory.ps1 Z: 14

$path = $args[0]  # The target directory or drive to clean.
$limit = (Get-Date).AddDays(-$args[1])  # Calculate the cutoff date.

# Displaying the action to the user
Write-Host "Removing files/folders older than: " $limit

# Listing, filtering, and removing files older than the limit
Get-ChildItem Path $path -Recurse | Where-Object {($_.LastWriteTime -lt $limit)} | Remove-Item

Scheduling to run periodically


To further enhance the utility of the cleanDirectory.ps1 PowerShell script, integrating it with Windows Task Scheduler allows for automation at scheduled intervals, ensuring your directories remain clean without manual intervention. Here’s how to integrate and automate the script using Windows Task Scheduler:

Step 1: Prepare Your Script

Ensure your cleanDirectory.ps1 script is tested and saved in a known location on your system. For example, you might save it to C:/scripts/cleanDirectory.ps1.

Step 2: Open Windows Task Scheduler

  1. Press Windows Key + R to open the Run dialog.
  2. Type taskschd.msc and press Enter. This opens the Task Scheduler.

Step 3: Create a New Task

  1. In the Task Scheduler, navigate to the Action menu and select Create Task....
  2. In the Create Task window, give your task a meaningful name, like Disk Cleanup Automation.

Step 4: Configure the Trigger

  1. Go to the Triggers tab and click New....
  2. Choose when you want the task to start under “Begin the task”. For regular cleanups, you might select On a schedule.
  3. Specify the frequency (daily, weekly, monthly) and the exact time for the task to run.
  4. Click OK to set the trigger.

Step 5: Set the Action

  1. Move to the Actions tab and click New....
  2. Set Action to Start a program.
  3. In the Program/script field, type powershell.exe.
  4. In the Add arguments (optional) field, enter the following, adjusting the path and parameters as necessary:
    -ExecutionPolicy Bypass -File "C:/scripts/cleanDirectory.ps1" Z: 14

    This command tells Task Scheduler to run PowerShell, bypass the execution policy for this script (allowing it to run without changing system-wide settings), and execute your cleanDirectory.ps1 script with the arguments for your path and age limit.

  5. Click OK to save the action.

Step 6: Configure Additional Settings

  1. If needed, adjust settings in the Conditions and Settings tabs, such as stopping the task if it runs longer than expected or ensuring it only runs if AC power is connected for laptops.
  2. Under the General tab, you may select Run whether user is logged on or not if you want the script to run regardless of whether you’re logged in.

Step 7: Save and Run

  1. Enter your credentials if prompted (necessary if you chose to run the task whether you’re logged on or not).
  2. Click OK to save your task.
  3. You can right-click your task and select Run to test it immediately.