From f1e8ed4e248aa988407c2e36d1d66f61f4d32785 Mon Sep 17 00:00:00 2001 From: dyoder Date: Fri, 1 Oct 2021 17:57:35 -0400 Subject: [PATCH] update --- Remove-OldFiles.ps1 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Remove-OldFiles.ps1 diff --git a/Remove-OldFiles.ps1 b/Remove-OldFiles.ps1 new file mode 100644 index 0000000..14e6cd3 --- /dev/null +++ b/Remove-OldFiles.ps1 @@ -0,0 +1,14 @@ +## 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 ) { + LogMsg "Deleting all items older than ${OlderThanDays} days in ""${Path}""" + $files = Get-ChildItem $Path | Where { !$_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-${OlderThanDays}) } + Foreach ( $f in $files ) { + $msg = "Deleting {0}" -f $f.FullName ; LogMsg $msg + Try { Remove-Item $f -Force | Out-Null } + Catch { LogErr $_.Exception.Message } + } + } Else { LogMsg "Path does not exist: ""${Path}""" } +} Else { LogMsg "The value entered for ""OlderThanDays"" is not an integer" } \ No newline at end of file