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
1 2 3 4 5 6 |
#Delete all Files in “C:\Old Files”) $Path = "C:\Your\Folder" #Folder path which you want to delete $Daysback = "-7" #Add Number of Days to delete old files $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $Path| Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Force |
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”.
1 2 3 4 5 6 |
#Delete all Files in “C:\Old Files”) $Path = "C:\Your\Folder" #Folder path which you want to delete $Daysback = "-7" #Add Number of Days to delete old files $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Force |
To better suit we can be modified to -include or -exclude parameter to specific file types, like so:
1 |
Get-ChildItem –Path "C:\Your\Folder" –Recurse -include *.log | Where-Object { $_.CreationTime –lt (Get-Date).AddDays(-5) } | Remove-Item |
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
1 |
Remove-Item -Path "C:\Future4tech\DummyfiletoDelete.txt" |
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:
1 |
Remove-Item 'D:\temp\Test Folder1' |
Using PowerShell commands to delete all files
1 |
Remove-Item -Path "C:\Future4tech*.*" |
Using PowerShell commands to delete all files and folders
1 |
Remove-Item -Path " C:\Future4tech *.*" -recurse |
Using PowerShell command to delete all files forcefully
1 |
Remove-Item -Path " C:\Future4tech *.*" -Force |
Delete Files Older than X Days using Command Prompt
1 |
forfiles -P "C:\Your\Folder" -S -M *.* -D -7 -C "CMD /C DEL @path" |
-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.
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.
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?
This web site is my inspiration , very great style and design and perfect written content.
Perfectly pent subject matter, Really enjoyed studying.
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!
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!
Absolutely written subject matter, Really enjoyed reading.
Thanks for helping out, fantastic information.
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.
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!
I like this post, enjoyed this one appreciate it for posting.
a exceptional text