How to Delete Files Older Than 90 Days and Save A Copy

This script was created in response to a request from one of my departments. When a file is 90 days old, they want a specific file type from a specific folder. I'm not 'very' comfortable just removing all of the PDFs in case one isn't the type of file they wanted to be removed. So I wrote this script to copy them first. Then how to delete them?

Best Answered by

Dany· Answered on Feb 03, 2023

I believe file screens included with Windows Server may accomplish this without a script. However, I've never done it before, but if you want a "cleaner" approach to handling the work, it might be worth looking into PowerShell.

PowerShell is a Microsoft application for job automation and Windows configuration management. It is a more advanced version of Command Prompt. PowerShell is also used to perform file-related tasks, such as creation and modification. Specifically, files on the PC can be erased using specific cmdlets.

When you need to use PowerShell to delete a file, you'll almost certainly come across the Remove-Item cmdlet. This cmdlet is the standard for PowerShell file removal. Using Remove-Item with the Get-ChildItem cmdlet to read files and directories and the sophisticated PowerShell pipeline may significantly simplify things.

You can also use PowerShell to delete log files older than X days, like those generated by IIS web servers, to free up disk space, as you said in the problematic case. Here is an example of PowerShell to delete specific files after X days:

Get-ChildItem c:\temp | Select-Object Name,CreationTime,@{n='AgeInDays';e={(New-TimeSpan -Start $PSItem.CreationTime).Days}}

In this example, there are files located in c:\temp that are older than 14 days. The script above shows the Name, CreationTIme, and AgeInDays of each file in c:\temp.

Delete all files, regardless of name, type, or extension. Specific files must sometimes be expressly excluded or included in the deletion procedure. To be safe, use Get-ChildItem first to obtain the list of files to be destroyed. You can finally pipe the collection to the Remove-Item cmdlet once you're satisfied with the list of files to delete.

However, files and folders deleted in this manner will bypass the Recycle Bin and vanish entirely. They are not on your machine. So, suppose you accidentally destroyed some of your important files and folders. In that case, you can use EaseUS Data Recovery Wizard, a professional data recovery software, to recover deleted files or repair corrupted data and files.

Use EaseUS Data Recovery Wizard to recover deleted or lost files easily and quickly.

Tutorials on how to recover lost, deleted, or formatted data from HDD, SSD, USB, SD card, or other storage media on Windows and Mac.

People Also Ask

How to Recover Lost Videos from Insta360

Are you wondering how to recover accidentally deleted videos from your Insta360? It is not difficult to do that, you'll just need a video recovery tool that can recover data from an SD card.


Dec 23, 2022

Need a Deleted File Recovery Tool for Windows XP

While uninstalling a program on your computer or laptop, it is easy to lose files. You will need a data recovery program if you cannot find your lost files in Recycle Bin. We will recommend EaseUS Data Recovery Wizard to help you recover the deleted files on Windows XP.


Dec 29, 2022

How to Delete Files After X Days Using PS Script

Do you want to know how to delete a file with a PowerShell script? Stop worrying because this post will explain how to delete multiple files using the Remove-item cmdlet and how to delete files using the PowerShell script. Also, if you have mistakenly removed files, EaseUS Data Recovery Wizard can recover them.


Jan 06, 2023

Can I Delete the Default Files on My External HDD Newly Bought

What should you do when you get a new external HDD? Is it necessary to delete the default files on your HDD? You will find the answer in this passage.


Jan 29, 2023

Use Shell Script to Delete the Files from Directory Based on Date Parameter

You can use a Shell script to delete the files from the directory based on the date parameter, but it is risky and easy to lose essential files. We will show you how to delete these files older than 2000 on your computer.


Jan 19, 2023

Data Deleted by Mistake on Windows 7, What Can I Do

How can I restore files that have been permanently deleted? By hitting the Shift + Erase keys, you can delete files or directories from your computer. If you do this and need to retrieve permanently erased files, one of the various data recovery applications available is EaseUS Data Recovery Wizard.


Dec 29, 2022