This article will discuss how to delete files older than x years, days, hours, or minutes using PowerShell. We will do that by breaking down the task into four ideas. After going through the four ideas, we will create the final PowerScript script you can use to delete older values.
Idea 1: Getting a List of All Files
This is done using the Get-ChildItem cmdlet. For example,
You can use this line to get all files and folders in a directory:
1 |
Get-ChildItem -Path folder1 |
If you want to get the folders, subfolders, and files within a directory, issue the -Recurse parameter as follows.
1 |
Get-ChildItem -Path folder1 -Recurse |
And if you want to get files only, supply the -File parameter.
1 |
Get-ChildItem -Path folder1 -Recurse -File |
We can also filter the Get-ChildItem list using the -Filter option as follows:
1 2 3 4 |
# Get files and folders starting with the substring "image" Get-ChildItem -Path folder1 -Recurse -Filter "image*" # Fetch PNG files only Get-ChildItem -Path folder1 -Recurse -Filter "*.png" |
You can read more about Get-ChildItem in the documentation.
Idea 2: Reading the Creation or Modification Time for Files and Folders
You can use the Get-ChildItem cmdlet to read information about a file. Among the properties retrieved by the cmdlet are LastWriteTime and CreationTime. We need these properties to identify files older than x days.
1 2 3 4 |
# To get the creation time of the example1.png file (Get-ChildItem -Path .\example1.png).CreationTime # Example output: Saturday, January 7, 2023, 9:00:00 AM # Fetch the modification time for the example1.png file (Get-ChildItem -Path .\example1.png).LastWriteTime # Example output: Saturday, March 18, 2023, 9:29:56 AM |
You can also right-click on a given file and see the Creation Time and Modification Time on the Properties option. You should see the results matching the output of Get-ChildItem results.
Idea 3: Computing CutOff Date
The current date can be retried with the command
1 |
Get-Date |
Then get the cutoff date based on Years, Months, Days, etc., as shown below
1 2 3 4 5 6 |
(Get-Date).AddYears(-2) # Current date minus two years (Get-Date).AddMonths(-3) # Current date minus three months (Get-Date).AddDays(-7) (Get-Date).AddHours(-12) (Get-Date).AddSeconds(-118) (Get-Date).AddMinutes(-30) |
If we want to remove files older than 30 days, you need to remove files whose CreationTime or ModificationTime comes before Today’Date subtract 30 days, that is, (Get-Date).AddDays(-30).
Idea 4: Removing a File Older than 7 Days
This uses what we have done in Ideas 1, 2, and 3 to identify if the file should be removed, then remove it if it is older than x days. Removing a file is done using the Remove-Item cmdlet.
This code will remove the example1.png file if it is older than 7 days.
1 2 3 4 |
$file = Get-ChildItem -Path ".\example1.png" if ($file.LastWriteTime -lt (Get-Date).AddDays(-7)){ Remove-Item -Path $file -Verbose } |
If you want to do a dry run (viewing what will be removed without actually removing them) of Remove-Item, use the -WhatIf parameter on the cmdlet as follows
1 |
Remove-Item -Path $file -Verbose -WhatIf |
Let’s Put it All Together
After going through the four ideas above, this script will be easy to understand. This script deletes all files older than 30 days in the <folder> (you can pick a different duration using what we discussed in Idea 3).
1 2 3 4 5 6 |
# Define the base folder containing files we want to delete $folder_path = "C:\Path\To\Folder" # Cut off date - go back 30 days $cutoff_date = (Get-Date).AddDays(-30) # This uses the Last modified time; you can also use Creation time (See Idea 2) Get-ChildItem -Path $folder_path -Recurse | Where-Object { $_.LastWriteTime -lt $cutoff_date} | Remove-Item -Force |
In the code above, Get-ChildItem fetches all the files on the $folder_path and pipes the results to Where-Object, which will filter the list to get files that have not been modified in the last 30 days. The list is then passed to the Remove-Item cmdlet to remove the resulting list.
Important: If you want to automate removing old files using the PowerShell script presented here, you can add it to Windows Task Scheduler (reference).