samiam41
asked on
Del out dir older than 30 days using PowerShell
Hey Experts. Spending a little more time putting together a script for this mini-project. After copying the files to the network share, I want to delete out any folders that are older than 30 days (last line of code which I'm unsure of). That should help maintain the size of the backup directory.
$timeStamp = Get-Date -Format MMddhhmm
$destination = "\\fs01\\Backups\PRTG_Config\BACKUP_$timeStamp"
$source = "\\d10\c$\ProgramData\Paessler\PRTG Network Monitor\Configuration Auto-Backups"
$max_days = 14
$limit = 30
$newerThan = (Get-Date).AddDays(-1 * $max_days)
If (-not (Test-Path -Path $destination)) {
New-Item -ItemType Directory -Path $destination -Force | Out-Null
}
Get-ChildItem $Source | ForEach-Object {
Write-Host "'$($_.FullName)' ..."
If ($_.LastWriteTime -ge $newerThan) {
$_ | Copy-Item -Destination $destination -Force
Write-Host " ... copied to '$($Destination)'"
} Else {
Write-Host " ... skipped, too old."
}
}
Get-ChildItem -Path $destination -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.lastwritetime -lt $limit } | Remove-Item -Force
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
EXPERT CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.