14 lines
857 B
PowerShell
14 lines
857 B
PowerShell
## This script will delete all files in a directory which are older than the specified number of days.
|
|
## It will not search recursively for files and will only look in the directory specified.
|
|
|
|
If ( $OlderThanDays -is [int] ) {
|
|
If ( Test-Path $Path ) {
|
|
Write-Output "Deleting all items older than ${OlderThanDays} days in ""${Path}"""
|
|
$files = Get-ChildItem $Path | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-${OlderThanDays}) }
|
|
Foreach ( $f in $files ) {
|
|
$msg = "Deleting {0}" -f $f.FullName ; Write-Output $msg
|
|
Try { Remove-Item $f -Force | Out-Null }
|
|
Catch { Write-Error $_.Exception.Message }
|
|
}
|
|
} Else { Write-Output "Path does not exist: ""${Path}""" }
|
|
} Else { Write-Output "The value entered for ""OlderThanDays"" is not an integer" } |