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.

$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

Populære opslag fra denne blog

Microsoft Office 2016 and the AUTO_ACTIVATE property

Access Exchange online with Office 365 multi-factor authentication (MFA/2FA)

Disable logon and logoff events (event id 4624, 4625, 4634 (and all the other ones...))