2

I found a script that can delete files over X days old and changed it a bit so that I will scan different network folders to see if there are files that are older than X days. Now when I run the script with PowerShell ISE the script is working fine and doing what it should do. It is taking about one hour.

Now when I try to schedule the script with Windows Task Scheduler, the task starts and completes 10 seconds later, but the script didn't run. So were no files deleted?


Scheduled Task setting:

General

  • Run task with account that has logon as batch priviledges

  • Run whether user is logged on or not

  • Run with highest priviledges

Trigger

  • Daily run

Action

  • Program/script: Run a program C:\windows\system32\windowspowershell\v1.0\powershell.exe

  • Argument: -File “C:\scripts\deletefiles.ps1"


What am I doing wrong? Or why is it not running the script?

The script can be found here.

1 Answer 1

3

You need to Set-ExecutionPolicy so that the account running the scheduled task is allowed to execute PowerShell scripts:

Set-ExecutionPolicy -Scope LocalMachine Unrestricted

As per the help file for Set-ExecutionPolicy, a scope of LocalMachine sets the policy for all users of that computer. As opposed to if you just Set-ExecutionPolicy Unrestricted from your own user account, it only sets that policy for your own user account. Furthermore, there are also separate policies for 32-bit and 64-bit versions of PowerShell.

And if Set-ExecutionPolicy -Scope LocalMachine Unrestricted is too much of a security compromise to you, you could set it just for that one user by logging in as that user and doing it, or executing a process under that user's security context and setting the policy for that user.

Oh, and well, this probably doesn't need to be said, but we can't know if the user you've assigned to this scheduled task has the permissions necessary to delete stuff from those UNC paths.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .