How to Delete Files Older Than 30 Days Using PowerShell
If you’re currently spring cleaning your computer or managing server storage, you might need to delete files older than X days. PowerShell provides an efficient way to automate this task. Here, I’ll explain a PowerShell script to delete all files older than 30 days in a specified directory.
Table of Contents
Complete Script
## How to delete files older than 30 days using PowerShell ## #░█████╗░██╗░░░██╗██████╗░███████╗██████╗░██╗██╗░░░░░██╗░░░░░░█████╗░ #██╔══██╗╚██╗░██╔╝██╔══██╗██╔════╝██╔══██╗██║██║░░░░░██║░░░░░██╔══██╗ #██║░░╚═╝░╚████╔╝░██████╦╝█████╗░░██████╔╝██║██║░░░░░██║░░░░░██║░░██║ #██║░░██╗░░╚██╔╝░░██╔══██╗██╔══╝░░██╔══██╗██║██║░░░░░██║░░░░░██║░░██║ #╚█████╔╝░░░██║░░░██████╦╝███████╗██║░░██║██║███████╗███████╗╚█████╔╝ #░╚════╝░░░░╚═╝░░░╚═════╝░╚══════╝╚═╝░░╚═╝╚═╝╚══════╝╚══════╝░╚════╝░ # Specify the directory which contains the files $directory = "C:\Users\Username\FilesDirectory" # Set a variable equal to the today's date so as to count back 30 days from the current day (for deletion of old files) $date = Get-Date $dateThirtyDaysAgo = $date.AddDays(-30) # Get all files from the directory we specified $files = Get-ChildItem $directory # Go through all files in the list Foreach ($file in $files) { # If file was last modified more than 30 days ago, delete it if ($file.LastWriteTime -le $dateThirtyDaysAgo) { Remove-Item -Recurse -Force $file.FullName } }
Setting Up the Directory
First, we specify the directory containing the files we want to target. You can change this to any directory on your system:
$directory = "C:\Users\Username\FilesDirectory"
Getting the Current Date
We need to get the current date to determine the cutoff date for deletion. Here, $date
holds the current date, and $dateThirtyDaysAgo
holds the date 30 days before today:
$date = Get-Date $dateThirtyDaysAgo = $date.AddDays(-30)
Retrieving the Files
Next, we use Get-ChildItem
to get all files in the specified directory. This cmdlet lists all items in the directory:
$files = Get-ChildItem $directory
Deleting Old Files
Now, we iterate through each file in the directory. For each file, we check its last modified date (LastWriteTime
). If this date is older than 30 days (-le $dateThirtyDaysAgo
), we delete the file using Remove-Item -Recurse -Force
:
Foreach ($file in $files) { if ($file.LastWriteTime -le $dateThirtyDaysAgo) { Remove-Item -Recurse -Force $file.FullName } }
Explanation of Key Cmdlets and Parameters
Get-ChildItem
: Retrieves the files and folders in a specified location.-Recurse
: InRemove-Item
, it ensures that all files and subfolders are deleted.-Force
: AllowsRemove-Item
to delete read-only files and hidden items.Get-Date
: Fetches the current date and time.AddDays(-30)
: Subtracts 30 days from the current date.LastWriteTime
: Property of files that shows the last modified date.
Customizing the Script
You might want to modify this script to:
- Target a different directory: Change the
$directory
variable. - Use a different time frame: Modify the
AddDays
parameter. - Include subdirectories: Add the
-Recurse
parameter toGet-ChildItem
.
Conclusion
This PowerShell script is a powerful tool for managing files on your system, particularly for deleting files older than a specific number of days. Whether you’re stuck at home or managing servers, automating file cleanup can save time and keep your storage organized. Happy scripting!