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 22, 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 Accidentally Deleted Files in Windows 10

If you have deleted files in Windows 10 and find no way to troubleshoot, don't worry. You can use EaseUS Data Recovery Wizard to undelete, restore and recover deleted files from Windows 10; even the files have been permanently removed.

Dany

Feb 22, 2023

How to Delete Files Older Than Max Age

Do you want to remove the files you saved a few days ago? Or maybe you want to get rid of the stuff you downloaded last week on the max days. This post will look at various methods for deleting files from a max given date.

Dany

Feb 22, 2023

Script to Delete Files Older Than 7 Days

You can move files older than 7 days to another folder with a script or using command lines. Let us find out how to move the files instead of deletion.

Dany

Feb 22, 2023

Recover Deleted DLL Files and Fix DLL Not Found & Missing Error

Accidentally delete a DLL file, or are you having DLL files Not Found & Missing errors in your PC? Relax! Here in this article, you will find effective methods to restore deleted or lost DLL files and fix DLL Not Found & Missing errors effectively.

Dany

Mar 10, 2023

Recover Deleted Videos on Btrfs Volume from Synology NAS

Suddenly deleted your videos on Synology NAS? Don't worry; this article will introduce effective video recovery software - EaseUS Data Recovery Wizard, to help you recover deleted videos on Btrfs from Synology NAS. Read on to get detailed information.

Dany

Apr 25, 2023

My Saves Being Deleted in Hogwarts Legacy, How to Fix

If your Hogwarts Legacy progress and game save are gone or deleted. This post provides working ways to recover lost Hogwarts Legacy savings. EaseUS Data Recovery Wizard works best in recovering lost game data. Download this tool and restore your data as soon as possible.

Dany

Mar 10, 2023