Last Updated on August 7, 2025 by Arnav Sharma
The script is designed to manage and delete files based on their age. Specifically, it offers the option to delete files older than a specified number of days, such as 15 or 5 days. The script uses the path -recurse command, commonly referred to as recurse, to delve deep into directories, ensuring it scans all child folders where files are located, be it on a local machine or a shared drive path.
The primary function of the script is to identify and remove files that were created or modified before a certain date. It does this by comparing the fullname attribute of each file against the current date. If a file’s age surpasses the set threshold, such as files older than 5 days or files older than 15 days using specific criteria, it becomes a candidate for deletion.
For those who might be dealing with old log files or any content that’s been sitting untouched for the last 30 days, this script is the answer you’re looking for. It provides an efficient way in Windows PowerShell to delete files older than some days, ensuring that only outdated content is removed. If there are files you would like to delete or if you’re unsure about the age criteria, the script offers flexibility in setting the desired age limit using the -gt parameter.
# Prompt user for the location to scan
$location = Read-Host "Enter the location to scan for files and folders"
# Prompt user for the duration (older than how many days)
$days = Read-Host "Enter the duration (older than how many days)"
# Prompt user for the action (1 for delete, 2 for do nothing)
$action = Read-Host "Enter the action (1 for delete, 2 for do nothing)"
# Calculate the cutoff date
$cutoffDate = (Get-Date).AddDays(-$days)
# Find files older than the specified duration
$oldFiles = Get-ChildItem -Path $location -Recurse -File | Where-Object { $_.LastWriteTime -lt $cutoffDate }
# Find folders older than the specified duration
$oldFolders = Get-ChildItem -Path $location -Recurse -Directory | Where-Object { $_.LastWriteTime -lt $cutoffDate }
# Take action based on user's choice
if ($action -eq "1") {
$oldFiles | ForEach-Object {
Write-Host "Deleting file: $($_.FullName)"
Remove-Item $_.FullName -Force
}
$oldFolders | ForEach-Object {
# Check if the folder is empty before deleting
if ((Get-ChildItem -Path $_.FullName).Count -eq 0) {
Write-Host "Deleting folder: $($_.FullName)"
Remove-Item $_.FullName -Recurse -Force
} else {
Write-Host "Skipping non-empty folder: $($_.FullName)"
}
}
} elseif ($action -eq "2") {
Write-Host "No action taken. Here are the files older than $days days:"
$oldFiles | ForEach-Object { Write-Host $_.FullName }
Write-Host "Here are the folders older than $days days:"
$oldFolders | ForEach-Object { Write-Host $_.FullName }
} else {
Write-Host "Invalid action selected."
}
Usage:
PowerShell script to delete files older than 30 days can be invaluable for system administrators aiming to free up space or maintain organized directories. This script might utilize commands that focus on attributes like LastWriteTime -lt or CreationTime -lt to determine the age of files.
Whether you’re dealing with log files that are older than 6 months or just general files older than 15 days, PowerShell offers a versatile way to delete old content. For those new to this, understanding PowerShell basics is crucial. Commands might include -recurse to scan through child folders or -include to specify file types. By setting a DateToDelete or using parameters like file.fullname and PSIsContainer -and, one can fine-tune the deletion process. This example will use PowerShell to target files in two different folders, ensuring they’re older than a given date before deletion. If you’re looking for a comprehensive guide on how to use PowerShell to delete files based on their age, whether they’re older than 1 day, 5 days, or any amount of days, you’ve come to the right place. We hope this helps explain how to use PowerShell effectively for such tasks.
I help organisations secure their cloud infrastructure and stay ahead of evolving cyber threats. Microsoft MVP and Certified Trainer, author of Mastering Azure Security, and founder of arnav.au — a platform for practical Cloud, Cybersecurity, DevOps and AI content.
Frequently Asked Questions
The script calculates a cutoff date by subtracting the specified number of days from the current date using AddDays(-$days). It then compares each file's LastWriteTime attribute against this cutoff date, marking files with a LastWriteTime earlier than the cutoff as candidates for deletion.
Yes, the script uses the -Recurse parameter with Get-ChildItem to scan all child folders within the specified location. This works for both local machine directories and shared drive paths, ensuring comprehensive coverage of all nested directories.
The script checks if folders are empty before attempting deletion. Empty folders older than the specified duration will be deleted, but non-empty folders are skipped with a message indicating they cannot be removed while they contain files.
The script offers two actions: entering '1' will delete all files and empty folders older than the specified duration, while entering '2' will display a list of matching files and folders without taking any deletion action, allowing users to preview results first.
The script is highly flexible, allowing users to set any desired age limit in days through the -days parameter. Whether you need to delete files older than 1 day, 5 days, 15 days, 30 days, or 6 months, the script adapts to your specified threshold.