Deleting Old Files After <n> Days
Deletes older files such as old log files.
This code works well with the Start-Transcript command. See Logging Jobs with Start-Transcript
Example - Delete Log Files More Than 90 Days Old
Deletes files from file path named in the $RunLogsPath
variable.
(For more on $RunLogsPath
see Logging Jobs with Start-Transcript )
POWERSHELL
$KeepDays = 90
$limit = (Get-Date).AddDays(-$KeepDays)
Write-Host " Delete files older than $($limit)) in $RunLogsPath"
Get-ChildItem $RunLogsPath | Where-Object { $_.LastWriteTime -lt $limit } | Remove-Item -Force
NOTES
$KeepDays
can be set to any number of days.Lines 1 & 2 can be combined as
$limit = (Get-Date).AddDays(-90)