Delete All Files Older than x Days using PowerShell
Requirement: Automatically Delete Files Older than x Days using PowerShell.
How to Delete Older Files using PowerShell?
Deleting unnecessary files is important for keeping your computer and storage running smoothly. But manually deleting these files can be time-consuming, especially if you have a large number of files to go through. Fortunately, the Windows PowerShell scripting language provides an easy and efficient way to delete all files that are older than a certain number of days. In this blog post, we’ll explain how to use PowerShell to delete all files older than x days. Let’s get started!
PowerShell Script to Delete Older Files
To begin, you’ll need to create a Windows PowerShell script. This script will contain the commands needed to delete all files older than x days from a specific directory. To delete all files that are older than a certain number of days in PowerShell, you can use the Get-ChildItem
cmdlet to retrieve the files, and the Where-Object
cmdlet to filter the files based on their age. You can then use the Remove-Item
cmdlet to delete the filtered files.
Here’s an example of how you can do this:
#Parameters
$Path = "C:\Temp" # Path where the file is located
$Days = "30" # Number of days before current date
#Calculate Cutoff date
$CutoffDate = (Get-Date).AddDays(-$Days)
#Get All Files modified more than the last 30 days
Get-ChildItem -Path $Path -Recurse -File | Where-Object { $_.LastWriteTime -lt $CutoffDate } | Remove-Item –Force -Verbose
This will delete all files in the C:\Temp
directory and its subdirectories that have the last write time that is older than 30 days.
The first line of code sets the path for where the files are located (e.g., C:\Temp). The second line sets the number of days before today’s date (e.g., 30). You can adjust the value of the $days
variable to specify the number of days that should be used as the cutoff. You can also specify a different folder path by modifying the C:\folder
path. The third command subtracts the number of days set in the $Days from today’s date and stores and stores it in a variable called “$CutoffDate”. The fourth command looks through all folders within that path for any file that was last modified before “$CutoffDate” (i.e., more than 30 days ago); if it finds such a file, it deletes it using the “Remove-Item” command. We have used the -Force
parameter to suppress this prompt and delete the files without any confirmation.
Delete Old Files based on the Creation Date using PowerShell
How about deleting old files based on their creation date? For example, instead of the last modified date – Let’s use the “Creation Date” as deletion criteria:
Get-ChildItem C:\Temp -Recurse -File | Where {$_.CreationTime -lt (Get-Date).AddDays(-30)} | Remove-Item -Force
This will delete all files created more than 30 days ago without prompting for confirmation.
Once you’ve created your script, save it, something like “DeleteOldFiles.ps1” for reference purposes later on. You should now have everything ready to run your script now! You can run it by opening up Windows PowerShell and entering the following command into it: ./DeleteOldFiles.ps1
. This will execute your script and delete all files in that folder that are older than 30 days old based on their “last write time” attribute (i.e. when they were last modified) or “created time”. If you want to change how many days old a file needs to be before it gets deleted, simply go back into PowerShell ISE, Notepad, or your text editor of choice and modify the variables to whatever value you wish (e.g., 10). Once done, rerun your script again with ./DeleteOldFiles.ps1
Wrapping up
If you have thousands or even millions of files stored on your computer or network drive, manually deleting them can be time-consuming and tedious work – but not anymore! By utilizing Windows PowerShell script, you can easily automate this process so that all files older than x days get deleted automatically without hassle! Just remember to double-check each step along the way when creating your own scripts; doing so will ensure that everything runs smoothly in the end!
In this article, we showed you how to delete all files older than x days using PowerShell. We used the Get-ChildItem and Remove-Item cmdlets to accomplish this task. This can be a huge time saver for archiving/removing old log files, backups, etc.
Many thanks
Just perfect, worked and fitted my needs. I just wanted to say thanks!
Is it possible to automate the deletion old versions of all files in a document library folder? I’m trying to clean up some folders to reclaim space but don’t know how to automate the deletion of previous file versions (while keeping current version only)?
Your script to delete files created more than 30 days ago actually deletes files created LESS than 30 days ago.
Change the -gt to -lt do delete files created more than 30 days ago.
Fixed it. Thanks for the catch!
Raj, I used the above to create my own powershell function which I can call and supply path and number of 90 days to purge. Hope this helps others
You can add/remove the PurgeOldFiles calls at the end as you need to cycle through multiple directories.
#Script Start
function PurgeOldFiles ($Days = 90,$Path = ‘C:\Temp’)
{
#Calculate Cutoff date
$CutoffDate = (Get-Date).AddDays(-$Days)
“Removing files in $Path over $Days days old”
Get-ChildItem -Path $Path -Recurse -File | Where-Object { $_.LastWriteTime -lt $CutoffDate } | Remove-Item –Force -Verbose
}
PurgeOldFiles 30 ‘C:\FolderName1’
PurgeOldFiles 90 ‘g:\FolderName2’
PurgeOldFiles 60 ‘D:\FolderName3’
#Script End
hi Clint,
Thanks for reply. suppose in C:\Temp folder there are many subfolders and i want to exclude some subfolders .
How to exclude those subfolders in below command
Get-ChildItem -Path $Path -Recurse -File | Where-Object { $_.LastWriteTime -lt $CutoffDate } | Remove-Item –Force -Verbose
Hi Salaudeen,
my requirement is to delete files automatically from two different folders in a shared drive path .files older than 6 month from folder and files older than 3 months from other folder. Kindly help me with the power shell script or batch script for this requirement.