-1

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:

https://blogs.technet.microsoft.com/heyscriptingguy/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script/

https://www.metalogix.com/help/Content%20Matrix%20Console/SharePoint%20Edition/002_HowTo/004_SharePointActions/012_SchedulingPowerShell.htm

ProphetSe7en
  • 465
  • 1
  • 4
  • 4
  • Please provide some more information on what your script is doing, and how the scheduled task is configured. Screenshots and code allow us to help. Saying what tutorials you've tried means people can eliminate things that didn't work for you. – G42 Apr 27 '17 at 08:35

2 Answers2

0

Have you set the Execution Policy?

Use an admin powershell and execute:

Set-ExecutionPolicy Unrestricted

Or have you set the "start in" directory to the script location?

S.Spieker
  • 7,005
  • 8
  • 44
  • 50
0

We really need more information on your script to be able to help. The biggest issue I've run into is scripts that interact with external objects. For example, sending an email message, running a macro through PowerShell on Word or Excel, etc. I work around that by either calling them through a batch file, or setting it to run when someone is logged in. Another issue I've seen is if I don't explicitly state the path. H:\script.ps1 versus \\server\file\myhomedrive\script.ps1.

Also, I call scripts from the task scheduler using start a program, Program/script C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe arguments -file "\\server\file\script.ps1"

Using just PowerShell.exe as the Program/Script has worked spottily at best, always use the full path if possible.

Finally, when a scheduled task runs it should give you a Last Run Result. What do you get there?

Nick
  • 1,178
  • 3
  • 24
  • 36