Delete files older than with PowerShell
I run this script on a Windows server to delete files older than X days. The files are backup files from a LINUX/Ubuntu server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Now = Get-Date | |
$Days = “90” | |
$TargetFolder = “E:\BACKUP" | |
$LastAccess = $Now.AddDays(-$days) | |
$Files = get-childitem $TargetFolder -include *.* -recurse | Where {$_.LastAccessTime -le “$LastAccess”} | |
foreach ($File in $Files) { | |
write-host “Deleting File $File” -foregroundcolor “Red”; Remove-Item $File | out-null | |
$LastAccessTime = $File.LastAccessTime | |
$FileSize = $File.Length | |
} |
Kommentarer
Send en kommentar