In this article, I will explain how to use PowerShell to delete files older than x days from your systems. Windows file explorer does not have an option to delete files older than x days, we have to manually remove files from the system.PowerShell Remove-Item cmdlet makes it easier to remove files from the system.
Remove-Item cmdlet in PowerShell is used to remove files one more item. Use the Get-ChildItem command to get desired files and use the pipeline operator to remove files using the Remove-Item cmdlet.
You can use the Remove-Item cmdlet to delete a file using the below command
Remove-Item -path D:\PowerShell\Temp.txt
This command deletes the Temp.txt file from the system.
Delete Files Older than 15 Days
You can use the Remove-Item cmdlet to delete files older than 15 days in combination with Get-ChildItem to get desired files from the system.
Use the below command to remove files older than 15 days using PowerShell
Get-ChildItem –Path "D:\PowerShell\Backup\" –Recurse | Where-Object { $_.CreationTime –lt (Get-Date).AddDays(-15) } | Remove-Item
In the above PowerShell script,
Get-ChildItem cmdlet in PowerShell gets files from the specified directory and using recurse parameters it recursively gets files from folders and subfolders.
It passes objects through the pipeline operator to the second command.
The second command uses Where-Object to use filter condition for files having CreationTime is older than 15 days and it passes files older than 15 days objects to the third command.
The third command uses the Remove-Item cmdlet to delete files older than 15 days.
Delete Files Older than 30 days with Extension
Often we have log files or backup files in a system older than 30 days and not in use or required at all, we need to find these files and remove them to free up disk space.
You can use the Get-ChildItem cmdlet to get a list of files older than 30 days with extensions like .log and use the Remove-Item cmdlet to delete files older than 30 days.
Get-ChildItem –Path D:\PowerShell\Backup –Recurse -include *.log | Where-Object { $_.CreationTime –lt (Get-Date).AddDays(-30) } | Remove-Item
In the above PowerShell script,
Get-ChildItem cmdlet uses Recurse parameter to recursively get the files with extension .log from the specified path.
It gets files that match pattern *.log and passes the object to the second command.
The second command uses Where-Object to check file creation time older than 30 days using Get-Date and pass an object to the Remove-Item cmdlet.
The third command uses Remove-Item to delete files older than 30 days with extension .log
Cool Tip: How to delete files if exists using PowerShell!
Conclusion
I hope the above article on how to delete files older than 15 days using the Remove-Item cmdlet is helpful.
You can use the Get-ChildItem cmdlet to get a list of files that matches the pattern to get specific extension files or get files older than 1 hour or 30 days.
Using Remove-Item cmdlet in PowerShell delete files older than x days.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.