Delete old log files with PowerShell
Note: This article was originally written in August 2015 and is now over 9 years old. Due to changes in PowerShell and Windows management over time, the described solution may need adaptation for current environments. Please consider this guide as a conceptual reference rather than a current implementation guide.
Today I came across a folder full of log files created by an import/integration application on one of our servers. The folder contained over 50,000 files. Rather than manually delete the old logs, here’s how I removed all items older than 1 month:
Get-ChildItem "C:\Path_To_logs\*.txt" | Where-Object {$_.CreationTime -lt (Get-Date).AddMonths(-1)} | Remove-Item -Force -Verbose -WhatIf
Since PowerShell is based on the .NET Framework, you can use standard System.DateTime methods when working with dates and times.
PS C:\> Get-Date | Get-Member Add*
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method datetime Add(timespan value)
AddDays Method datetime AddDays(double value)
AddHours Method datetime AddHours(double value)
AddMilliseconds Method datetime AddMilliseconds(double value)
AddMinutes Method datetime AddMinutes(double value)
AddMonths Method datetime AddMonths(int months)
AddSeconds Method datetime AddSeconds(double value)
AddTicks Method datetime AddTicks(long value)
AddYears Method datetime AddYears(int value)
Passing a negative value to one of the Add* methods will result in subtracting that amount of time:
PS C:\> Get-Date
Wednesday, 12 August 2015 2:37:23 PM
PS C:\> (Get-Date).AddMonths(1)
Saturday, 12 September 2015 2:37:37 PM
PS C:\> (Get-Date).AddMonths(-1)
Sunday, 12 July 2015 2:37:40 PM
Obviously you need to be very careful with specifying a path to a command like Remove-Item. I’ve left the -WhatIf switch on the example code above.
It’s easy to add something like this to a scheduled task to keep a log folder tidy.
2025 Update:
Modern Considerations:
In newer PowerShell versions, you might prefer using LastWriteTime instead of CreationTime as it’s more reliable for determining when a file was last modified:
Get-ChildItem "C:\Path_To_logs\*.txt" | Where-Object {$_.LastWriteTime -lt (Get-Date).AddMonths(-1)} | Remove-Item -Force -Verbose -WhatIf
Security Considerations:
- Always review the results with -WhatIf before running the actual deletion
- Consider archiving important logs before deletion
- Ensure proper permissions when automating via scheduled tasks
- For sensitive systems, implement logging of the deletion process itself
- Test scripts thoroughly in non-production environments first