Deleting Old Files After <n> Days

Deletes older files such as old log files.

This code works well with the Start-Transcript command. See https://dataself.atlassian.net/wiki/spaces/DS/pages/1673723977

Example - Delete Log Files More Than 90 Days Old

Deletes files from file path named in the $RunLogsPath variable.
(For more on $RunLogsPath see https://dataself.atlassian.net/wiki/spaces/DS/pages/1673723977 )

$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)