Windows – My PowerShell script won’t save to file when running using Task Scheduler; do I need to specify a specific argument

automationpowershellscheduled-taskwindowswindows 7

I have a script that downloads a temporary Excel file, copies parts of it to a new file, and saves it to a specific location on the network.

The problem is that the new file is never created/saved. If I run the script locally (through cmd.exe, PowerShell, or PowerShell ISE), it WILL save the file locally, or to the network. If I try running the script via a schedule or on-demand via Task Scheduler, the temporary file is created, but the final document is never created or saved. Is there a specific argument I need to pass, or anything I could be doing wrong? This is the command I'm currently using:

    powershell.exe -file C:\path\to\my\powershell\script\thescript.ps1

Since it calls environment variables, and other variables relative to the scripts positon, I also set "Start in" to

    C:\path\to\my\powershell\script\

I have tried using

    \\MYSERVER\Path\To\Directory\file.xlsx

as the location for the file on the network (as suggested here), but this does not work either.

Best Answer

The scripts works when you are running it because YOU have rights to the destination location. By default, a scheduled task runs as the local system user.

You should create a new, dedicated user in your domain for this purpose, give it exactly the rights it needs to complete this task (and no more), and configure your scheduled task to run as that user.