I am trying to get the below PowerShell script to work using Task Scheduler. The problem is that it wont delete any files.
When I run it manually it needs a confirma
You can simplify your script like it:
-file with Get-Childitem command$Now variableif must be out for check if no files-Force to your Remove-Item command'\*.*'Code ratified:
$Days = "10"
#----- define folder where files are located ----#
$TargetFolder = "D:\Shares\Downloads\TV\AutoDL"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = (Get-Date).AddDays(-$Days)
#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $TargetFolder -Recurse -file | Where LastWriteTime -le "$LastWrite"
if ($Files -eq $null)
{
Write-Host "No more files to delete!" -foregroundcolor "Green"
}
else
{
$Files | %{
write-host "Deleting File $_" -ForegroundColor "DarkRed"
Remove-Item $_.FullName -Force | out-null
}
}