Apparently, I'm not the only one who wondered why a script run manually worked fine but the same script run from Task Scheduler seemed to run but didn't actually do anything.

Firstly, Task Scheduler doesn't seem to care too much if the task fails. It records the whole event as "Action Started" and "Action Completed". This is confusing since if you think the Task Scheduler should report your error but doesn't, it must have run the script right? Nope.

Anyway, forget getting much help from Task Scheduler, although, if you look closely in the history tab of the task and find Action Completed, you will find a return code logged in the history item. If the error code is 4294770688, then it might simply be that you need to pass arguments in double-quotes, not single quotes!

How do you debug this whole thing?

Firstly, it does work, it is just about setting it up correctly. Start with a simple powershell script that creates a random file like New-Item "C:\temp\test.txt" and nothing else. Once you get this to run correctly, any other problems are related to your script.

Secondly, although running it locally might look like it works correctly, when I did this, I was not calling it exactly as the Task Scheduler was. When testing locally, I was simply calling powershell.exe .\myscript.ps1 whereas the task scheduler was doing something more like powershell.exe -File 'c:\full\path\to\myscript' If I had run this exactly as Task scheduler, I would have probably seen the error and worked it out!

Thirdly, try not to assume the current directory when running your script. Simply include the full path to it and make sure it isn't inside a user directory if you are not running the script as that user otherwise access might be denied.

Fourthly, some people have talked about telling powershell to bypass its execution policy (-ExecutionPolicy bypass) but this depends on what is setup on the machine globally. If it runs locally, it should run from task scheduler without any changes to the arguments.

Firth, make sure the user you are running the task as has permissions to do what the powershell script needs to do. If at all possible, login as the other user and run it to make sure. Running as system accounts is OK but don't assume that they will automatically get super permissions to do everything.

Sixth, any calls to things like GetUserDirectory or GetTempDirectory() will either return different values for different users or potentially won't work at all if the account is logged off. Again, assume nothing, start with something simple and work your way up to finding out what is going on.

Enjoy!