So, you want to delete files that:
- Are 10 days old or older AND have only been archived (have the 'A' attribute and nothing else)
$dir = "C:\Temp"
$tenDaysAgo = (Get-Date).AddDays(-10)
if (Test-Path -Path $dir)
{
Get-ChildItem -Path $dir -Filter "*.gsa" -Recurse | Where-Object {
($_.LastWriteTime -le $tenDaysAgo) -and
($_.Attributes -eq [System.IO.FileAttributes]::Archive)
} | ForEach-Object {
Write-Host "Deleting " -NoNewLine
Write-Host $_.FullName -ForegroundColor Blue
Remove-Item -Path $_.FullName
}
}
else
{
Write-Host "$dir does not exist." -ForegroundColor Red
}
Get-ChildItem -Path $dir | Where-Object { $_.PsIsContainer } | ForEach-Object {
if (($_.GetFiles().Length -eq 0) -and ($_.GetDirectories().Length -eq 0)) {
Remove-Item -Path $_.FullName -Verbose
}
}