Powershell Delete old files and logging
Lösche Dateien und erstelle ein LogFile
Mit diesem Powershell-Skript löscht ihr Dateien älter als X Tagen und setzt einen Filter für Dateien die gelöscht werden sollen inkl. eventueller Ausnahmen.
Powershell anzeigen
$delete_path = „F:\Daten\*“
$ignore_Path = „F:\Daten\Test“
$report_path = „C:\LOG“
$olderthan = -10
$del_date = (Get-Date).AddDays($olderthan)
$includefiles = @(„*.txt“,“*.log“,“*.csv“,“*.docx“,“*.PDF“)
$excludefiles = @(„*.ps1″,“*.vsdx“)
$Delete = Get-ChildItem $delete_path -Inc $includefiles -Excl $excludefiles -Recurse |
Where-Object { !($_.PSIsContainer) -and
$_.LastWriteTime -lt $del_date -and
$_.Directory -notlike $Ignore_Path }
if($Delete){
Set-Content „$report_path\$(get-date -uFormat „%d-%m-%Y %H-%M-%S“).log“ $Delete;
Remove-Item -Path $Delete -Force -Recurse
}

