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

12 Responses

  1. 메이저사이트추천 says:

    It is really a nice and useful piece of info. I?m glad that you shared this useful information with us. Please keep us informed like this. Thank you for sharing.

  2. zoritoler imol says:

    Wow! Thank you! I continuously needed to write on my site something like that. Can I include a portion of your post to my website?

  3. zoritoler imol says:

    This web site is my inspiration , very great style and design and perfect written content.

  4. gralion torile says:

    Perfectly pent subject matter, Really enjoyed studying.

  5. tracfone coupon says:

    Thanks for the marvelous posting! I definitely enjoyed reading it, you are a
    great author. I will be sure to bookmark your blog and definitely will come back from now on. I want to encourage you continue
    your great writing, have a nice evening!

  6. zoritoler imol says:

    I’d have to examine with you here. Which is not one thing I usually do! I take pleasure in reading a post that may make folks think. Additionally, thanks for permitting me to comment!

  7. zoritoler imol says:

    Absolutely written subject matter, Really enjoyed reading.

  8. zoritoler imol says:

    Thanks for helping out, fantastic information.

  9. zoritoler imol says:

    Hi there very cool blog!! Man .. Beautiful .. Amazing .. I’ll bookmark your blog and take the feeds additionally…I am happy to search out so many useful information here within the put up, we want develop more techniques on this regard, thanks for sharing.

  10. graliontorile says:

    Heya this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience. Any help would be enormously appreciated!

  11. gralion torile says:

    I like this post, enjoyed this one appreciate it for posting.

  12. replicamagic.co says:

    a exceptional text

Leave a Reply

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