0

I'm attempting to create a task via powershell to delete some files older then 6 hours, if I execute the script from powershell there are no issues, if I try to execute from task scheduler nothing happens..

Call the Powershell.exe in my schedulded task:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Set this Parameters :

-NoProfile -ExecutionPolicy Bypass -Command -NonInteractive -File "C:\Scripts\DeleteFilesDiff3H.PS1"

What could be the problem of the task scheduler not launching my script?

Tried to aply some solutions provide to similar issues without success

$Path = "E:\MyPath"
$now = Get-Date

Get-Childitem * |
Where-Object { $_.LastWriteTime -le $now.AddHours(-6) } |
Remove-Item -Recurse -Force

I got this messages:

Task Scheduler started "{38dcd44b-4210-473b-921e-3cc1442ff03b}" instance of the "\Delete Files 3H" task for user "my user".

Task Engine "S-1-5-21-159114655-2248028564-2417230598-213599:My User:Interactive:LUA[2]"  received a message from Task Scheduler service requesting to launch task "\Delete Files 3H" .

Task Scheduler launched "{38dcd44b-4210-473b-921e-3cc1442ff03b}"  instance of task "\Delete Files 3H" due to a time trigger condition.

Task Scheduler successfully completed task "\Delete Files 3H" , instance "{618e6f44-b523-4c56-ae0b-04d3552391cc}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" with return code 0.
2
  • 3
    Use either -Command or -File, which serve distinct, mutually exclusive purposes. In your case, try removing -Command. Commented Jun 2, 2019 at 22:04
  • Hi mklement0 thanks for your help. Removing -Command don't change the output, even the task completion with success no file are deleted. Commented Jun 3, 2019 at 6:14

1 Answer 1

4

You don't make use of the defined variable $path so Get-ChildItem will never look there. Update your code to the following and check if this works for you:

$Path = "E:\MyPath"
$now = Get-Date

Get-Childitem -path $Path |
Where-Object { $_.LastWriteTime -le $now.AddHours(-6) } |
Remove-Item -Recurse -Force
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.