Scheduled Task to run PowerShell script

I’m trying to create a task in Task Scheduler that will run a PowerShell script at 12:00 am every day and it’s not working, here are my settings: (see attach).
I’m also including a copy of the script file in notepad. Can anyone help? Thanks.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Does it work when you manually run the task?

Windows 7, Server 2008, etc. is very finicky with running scheduled task because we find that you cannot be logged off or the task won't run for example.  Some others claimed making the task to run as administrator and wake computer up automatically, etc. worked for them.

First and foremost, set the task to run as a user with administrator privileges.
Will SzymkowskiSenior Solution ArchitectCommented:
You have the incorrect syntax in the Task scheduler to run this script. You cannot call a .PS1 script separately. You first need to call powershell. So it should look like below...

Details should look like below...
powershell.exe -file "c:\filepath\scriptname.ps1"

Once you have it set like above it should run successfully.

narce100Author Commented:
Wayne88: Yes I run the script manually every day for quite some time but now I need to run it late at night.

Will: I'll try that syntax and see if it works.
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

narce100Author Commented:
I wrote a shorter and easier version of the report.ps1 file, it ran well manually but when I set up the trigger to run a few minutes after my current time. It does not seem to work, but the log shows that it did. Attached is a print screen with the settings and a notepad copy of the file.
Will SzymkowskiSenior Solution ArchitectCommented:
Another thing you can try is just call the powershell script from a batch file.

Joshua GrantomSenior Systems AdministratorCommented:
Will is correct on his post about using

powershell.exe -file "c:\filepath\scriptname.ps1"

Here is what it should look like.

narce100Author Commented:
The task ran and was completed according to Task Scheduler history log; but I don't see the script run or the files that are supposed to generate are not on the designated folder see attach for settings and script file
Joshua GrantomSenior Systems AdministratorCommented:
What account are you running the task under? If the S Drive is not a local drive and it's actually a network drive then you will need to mount S: within the script as well
narce100Author Commented:
I already have a map on my desktop to the S: drive and when creating the Task I sign in as myself (Domain Administrator) and the logon window disappears after that.
It runs fine when I do it manually; do I have to write the entire path instead of the mapping on the script?
Joshua GrantomSenior Systems AdministratorCommented:
Yes I would. Including the entire network path will reduce the points of failure. Did you put in your credentials so that the task will run using your active directory account?
narce100Author Commented:
Yes, actually a window comes up every time you modify something on the task scheduler and prompts you to type username/password under the task will run on.
I will write the entire path and keep you posted on the results
narce100Author Commented:
I typed the entire path and the task since to run with no problem is the PowerShell script that does not run, I don't see it come up or anything.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The S: drive cannot be used in the task, so using the UNC path is correct.
But since the file is not generated, and the task runs for 2 minutes, something is executed, but either there is no result or the file cannot get created.
Put a sleep 300 at the end of the PS script, set the task to run only if you are logged in, and then kickstart the task. You should see the window now for about 7 minutes. In particular you should see if errors are encountered.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
narce100Author Commented:
It worked, I have some fine tune to do in the script but is fine.

Thank You.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.