Warum funktioniert mein Powershell Script nicht?

Hallo,

ich hab mir ein kleines Powershell Script geschrieben, um alte Dateien automatisch zu löschen. Das einzige was ich noch hinbekommen will ist, dass ich bestimmte Schlüsselwörter in den Dateinamen setze und diese dann nicht gelöscht wird. Bis auf das letzte funktioniert alles und ich verstehe nicht, warum auch Dateien mit Schlüsselwort im Dateinamen gelöscht werden. Vielleicht kann ja jemand helfen

function cleanUp($path) {
    if ($path -notmatch '\\$') {
        $dir += '\'
    }
    
    $dirs = Get-ChildItem $path -directory -recurse | Where-Object { (Get-ChildItem $_.fullName).count -eq 0 } | Select-Object -expandproperty FullName
    $dirs | Foreach-Object { Remove-Item $_ }


}


function deleteFiles($path, $days, $hours, $minutes, $exclude) {
    if ($path -notmatch '\\$') {
        $dir += '\'
    }
    $files = @(Get-ChildItem -Path $path -Recurse -ErrorAction SilentlyContinue)
    $timespan = new-timespan -days $days -hours $hours -minutes $minutes


    foreach ($i in $files) {
        if (((get-date) - $i.LastWriteTime) -gt $timespan) {
            
            if ($i -notcontains $exclude) {    
                Remove-Item $i.FullName -Recurse -Force -ErrorAction SilentlyContinue
            }
        }
    }
    cleanUp($path)
}


deleteFiles -path "C:\Temp" -days 0 -hours 0 -minutes 05 -exclude "SAFE"
Computer, Windows, Microsoft, Schule, programmieren, Scripting, PowerShell

Meistgelesene Fragen zum Thema PowerShell