PowerShell Basics: How to Delete Files Older Than X Days

Sometimes we have to delete the number of files and folders or delete the file inside the folders. It is very difficult to find and delete files with windows file explorer files based on their date & time or particular file extension, name.

However, we can easily do it in command line with the help of Windows PowerShell and Command Prompt. This is the easy way to search the file inside the folders based on date & time, extension & name.

In some cases, we can schedule the PowerShell & Batch script at a particular time to delete the old files. I am generally using this type of script for deleting the old database backups, like delete the last 60 days’ backup files inside the sub-folders according to date & time. This has helped me to free up space and keep things organized only deleting files that are older than a certain number of days in any folder or sub-folder.

Delete Files Older than X Days using Windows PowerShell

Remove-Item cmdlet is used to delete a directory by passing the path of the directory to be deleted.

In the above command, remember to change "C:\Your\Folder" specifying the path to the folder that you want to delete files, and change “-7” to select files with a last modified date.

If you want to delete files in sub-folders too, you can use below script. This is the same script with the Get-Childitem parameter “-Recurse”.

To better suit we can be modified to -include or -exclude parameter to specific file types, like so:

This will delete only those files that have *.log extension. To exclude certain files, use -exclude parameter.

In addition to CreationTime you can also delete files based on LastAccessTime or LastWriteTime (Modified) parameter.


Using PowerShell Commands to delete a file

The above command will execute and delete the “DummyfiletoDelete.txt” file, which presents inside the “C:\Future4tech” location.

We can delete a folder D:\Temp\Test Folder1 by using below command:

Using PowerShell commands to delete all files

Using PowerShell commands to delete all files and folders

Using PowerShell command to delete all files forcefully

Delete Files Older than X Days using Command Prompt

-P: Specifies path to the folder that contains your files.
-S: Recurse into any subfolders.
-M: Search all files or limit to specific file types. Specify file type like this: *.log for all log files.
-D: Specifies modified date.
-C Run command enclosed in quotes.

To know more about DOS command, please visit DEL & Forfiles

Deleting files from the Windows Command line can be quite dangerous if you aren’t careful. It’s better to run the script or command in test files before run in the actual files. When a file is deleted from the command line, it skips the Recycle Bin altogether and works like the Shift + Delete option while using the Explorer.

You may also like...

1 Response

  1. replicamagic.co says:

    a exceptional text

Leave a Reply

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