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:

$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.

Delete All Files Older than x Days using PowerShell

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 “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 last modified date – Let’s use the “Creation Date” as deletion criteria:

Get-ChildItem C:\Temp -Recurse -File | Where {$_.CreationTime -gt (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.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *