p/s scripts and task scheduler windows 10

Ok I’m sure this is a no brainer for you guys but this just really stumps me. I have a simple script that deletes files out of a temporary file folder I have setup for multiple video editing software. I can run it in p/s admin and it works. I can add and execution bypass in non admin and it works. but I can’t get it to run in task scheduler. here is my script. 0 days was set just to test. I haven’t determined how many days I want temp files there before deletion yet. I have t/s setup to run at highest privileges I enter the password I have tried calling on this script setup as a .ps1 file to be opened with p/s and I have called on p/s and entered this as an argument. I am much more on the hardware side of computers and I use p/s alot for diskpart and robocopy…so I’m pretty clueless otherwise.

Set-ExecutionPolicy -ExecutionPolicy AllSigned -Scope process
Get-ChildItem -Path “E:\Temporary Files\Temp” -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(0))} | Remove-Item

How do you start the script in the task scheduler? What’s command line?

Why do have this line in your script? “Set-ExecutionPolicy -ExecutionPolicy AllSigned -Scope process”

Is your script digitally signed?

I came to using all signed because it didn’t kick an execution error when running out of admin. Script is not signed. I have ts set to run daily if user is logged on or not and to be able to run on demand. I haven’t even seen if the task will run on its own yet I am simply hitting run while the task is selected in ts.

And honestly you can tell me it’s all wrong. I have done coding at a very low level for java,visual basic and sql but it’s been a while. I got so frustrated last night with it I can’t say if I made progress or not. All I know is the script that actually picks the files and deletes them does work ran manually.

OK, first - delete the line with the Set-ExecutionPolicy. It does not make any sense or serv any purpose for your script.

Could please share the complete command line you’re using to run your script in the task scheduler. Please format it as code using the code tags “PRE”. Thanks

Read Me Before Posting! You’ll be Glad You Did!

Sorry for my ignorance but I don’t understand what you asking. I am using the task scheduler gui and in the action tab I am calling on powershell.exe and adding my script as an argument.

OK. I’m assuming you’re using the “Action” - “Start a program”, right? What exactly do you have in “Settings” - “Program/script:”? And what do you have in “Add arguments (optional):”? And please do not describe it - copy it.

This actually worked but again your welcome to tell me that I need to do it differently. I have been putting random text documents into my temp folder to verify it is removing them.

Program/script : powershell.exe

Add Arguments: [pre]-ExecutionPolicy Bypass -File “C:\Users\Tom Lang\Documents\EditCleanup1.ps1” [/pre]

Code in EditCleanup.ps1 : [pre] Get-ChildItem -Path “E:\Temporary Files\Temp” -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(0))} | Remove-Item[/pre]

How "This actually worked … "? I understood that it does not work. Maybe I missunderstood your issue.

OK, your command line looks just fine and your script (except of the Set-ExecutionPolicy thing) works as well … I just tested it.

sorry you did understand me correctly. At the start of this I could not get it to work. While talking this over with you I was trying different things and came across something that worked. I do thank you for taking the time to work with me on this.

Great. I’m glad you could solve your problem.

Have a nice day.