I have tried every tutorials out there, but it wont run when set ut in scheduled task.
Can someone please tell me how to get scheduled task to run a powershell script on windows 10?
Edit:
Some additional info:
I am trying to run a script that delete files older than 10 days. Got some help with the script here.
How can I delete files with PowerShell without confirmation?
$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
}
}
In scheduled task I have these settings:
Run with highest privliges Program/script: I have tried "powershell", "powershell.exe" and "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Add Arguments: I have tried ".\nameofscript.ps1", -file "C:\Script\nameofscrips.ps1", -command and some other suggestions I found.
Start in: C:\Script
I tried the settings in these tutorials: