Delete files older than 30 days:
In many scenario we will store large number of non-important files on a different location, and its very difficult to delete those huge files old files monthly wise, to handle such scenario we are here going to use PowerShell and Task Scheduler to monitor and clean up files from any folder that are older than a specified number of days.
In this post, youโll learn the steps to automatically delete files that havenโt been modified in the last month or any number of days you specify on Windows. The below scripts will delete the files that havenโt been modified in the last 30 days.
1 2 3 4 5 6 7 8 |
################################################################# #Project : How to delete the old file automatically using Powershell #Developer : Thiyagu S (dotnet-helpers.com) #Tools : PowerShell 5.1.15063.1155 #E-Mail : mail2thiyaguji@gmail.com ################################################################## Get-ChildItem โPath "C:\PowerShell\OriginalFolder" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item |
In the above command remember to change “C:\path\to\folder” specifying the path to the folder that you want to delete files, and change -30 to select files with a last modified date.
How to use Task Scheduler to delete files older than 30 days automatically using Powershell:
In above code we have created Powershell script for deleting the files older than 30 day, but our scenario is want to delete automatically, Most we will use the graphic interface of Taskschd.msc console to create Windows Task Scheduler jobs and Building a single scheduled task via the GUI task scheduler might not be a big deal. But if you find yourself creating scheduled tasks repeatedly it might be a good idea to use a method that scales better than a GUI like PowerShell. However, in various scripts and automated jobs, it is much more convenient to use the PowerShell features to create scheduled tasks. In this below link, we have detail description about how to create new Windows Scheduler tasks using PowerShell.
.
Leave A Comment