If you want to maintain and organize files, then it is good to regularly delete files that are no longer needed, specifically files that are older than a certain number of days. In this tutorial, I will show you how to delete files older than X days in PowerShell.
To delete files older than a specified number of days in PowerShell, use the Get-ChildItem cmdlet to retrieve the files from a directory, filter them by last write time with the Where-Object cmdlet, and then remove them with Remove-Item. For example, to delete files older than 30 days, you could use this command: Get-ChildItem -Path “C:\MyFolder” | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item.
Delete Files Older Than X Days in PowerShell
PowerShell provides Get-ChildItem cmdlet to delete files older than a certain number of days.
When using this command, it first retrieves the files from a specified directory and then piping the output to the Where-Object cmdlet to filter the files based on their age. Finally, the Remove-Item cmdlet is used to delete the files.
Here’s a basic example of a script that deletes files older than 30 days from a specific folder:
$Path = "C:\MyFolder"
$Days = 30
Get-ChildItem -Path $Path | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$Days) } | Remove-ItemIn this script, $Path specifies the directory to search for files, and $Days defines the number of days used to filter files based on their last write time. Files older than 30 days from the current date are deleted.
In the screenshot below, I executed the script, which deleted all the files older than 30 days from the directory.

Read How to Use PowerShell to Find Files Modified After a Certain Date?
Delete Specific File Types Older Than 30 Days in PowerShell
You can also modify the PowerShell script to delete specific file types or exclude certain directories that are older than 30 days. For instance, to delete only .log files older than 30 days, you can use the following script:
$Path = "C:\Logs"
$Days = 30
Get-ChildItem -Path $Path -Filter *.log | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$Days) } | Remove-ItemThis PowerShell script will search for .log files specifically and exclude all other file types.
Handling Errors and Logging
It is always good to handle errors while writing the PowerShell script. You can add -ErrorAction and -ErrorVariable parameters to capture any errors that occur. Additionally, you can output the actions to a log file using Out-File or Add-Content cmdlets. Here’s how you can modify the PowerShell script to include error handling and logging:
$Path = "C:\MyFolder"
$Days = 30
$LogPath = "C:\Logs\log.txt"
$ErrorActionPreference = "Continue"
Get-ChildItem -Path $Path -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$Days) } | ForEach-Object {
try {
$_ | Remove-Item -ErrorAction Stop
"$($_.FullName) was deleted successfully" | Out-File -FilePath $LogPath -Append
} catch {
"An error occurred: $_" | Out-File -FilePath $LogPath -Append
}
}In the above PowerShell script, $LogPath specifies the path to the log file where the actions will be recorded. The try block attempts to delete the file and logs the success message, while the catch block captures any errors and logs them.
Conclusion
Using PowerShell to delete files older than a set number of days is easy and most convenient.
In this PowerShell tutorial, I have explained how to delete files older than X days in PowerShell using Get-ChildItem and Remove-Item cmdlets.
You may also like the following tutorials:
- Copy and Rename Files in PowerShell
- Copy Files from One Folder to Another in PowerShell
- Delete a File in PowerShell
- Find Strings in Files in PowerShell
- How to Check if a File Was Modified in the Last 24 Hours in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.