Using Task Scheduler to run a PowerShell Script

Although this is quite a straight forward thing to implement searching around on the web it did appear that there is some confusion around what to put where when specifying a PowerShell script from Windows Server Task Scheduler. 

So I thought I’d put together this brief post.  In this example I am using MS Windows Server 2008 to schedule a PowerShell to run.  If we were to try and run a PowerShell from a normal command line we’d type the following:

powershell.exe –Noninteractive –Noprofile –Command “&{<fullpath to script>}”

Notice that I didn’t specify the path to the powershell executable – the reason for this being that I have the path already specified in the servers Path environment variable, ie: %SystemRoot%system32WindowsPowerShellv1.0

If you haven’t already done this you’ll have to specify the full path to the PowerShell executable – this is a real faff (a technical term  :) ) so I’d recommend adding it your path environment variable if you haven’t done so.

When scheduling a new task and specifying the action (ie: in this case running a PowerShell script) the core of your syntax will be in the ‘Arguments’ section of the action (see below).

Use the image below as a template and enter the directory containing your PowerShell script in the ‘Start in’ setting field.

image

When completed you should have something similar to the action below.

image

Happy PowerShelling…  :)

 

Comments

  1. says

    I had just finished the creation of a script to take backups for my server and it wasn’t running on the scheduler at all. I read the article and I noticed what I had left out… The full path to the script I had used ‘./’ in a bat file.

    Thanks for writing the article. It helped me.

  2. Rashid says

    I needed to setup a scheduled task that ran some PowerShell script in Windows Server 2008. When searching the web I noticed that a lot of people had struggled with this and so I decided to write this post to explain how I managed it. The scheduled task will ‘run whether the user is logged on or not’. You can schedule a PowerShell script using task scheduler which will run automatically on a given time.

    http://blog.pointbeyond.com/2010/04/23/run-powershell-script-using-windows-server-2008-task-scheduler/

    • says

      Hi Rashid,

      Thanks for the comment and the link to your script. I’ll have to download it and give it a try sometime soon. I’m sure it’ll prove useful to others. :)

      Cheers,

      Simon

  3. says

    I need to automate the copying copying of database files from one server to another automatically on a weekly and monthly basis. I have tried your tips. with { without} nothing seems to work; however, the log says the script processed successfully. I tried with plan .bat files first that worked beautifully while the servers were 2003 servers sp2… I could really use a nudge! Both the .bat and the .ps1 files work when I manually start them- just not from within task scheduler.

    • Rich says

      Had the same problem. Found that running the cmd in a batch file with a pause showed the problem. Copying the command text from this site, although the dashes look correct, they are different and not understood by powershell. Delete and manually type the command line dashes (-noninteractive) and quotes to fix.

      • says

        Hi Rich,

        Thanks for the heads-up. It would appear that the line dashes when published in the post perhaps had some control characters or similar added.

        Thanks again for letting us know.

        All the best,

        Simon

  4. Greg says

    I’m still having a big issue with this…I can create the task, but it says “Could not start” when I try to run it.

    I’ve set the execution policy to unrestricted, and I can run the ps1 script from a powershell shell fine. I can also run the whole command as stated above in a cmd shell.

    The only thing I can think of is that perhaps the user the task runs as must have a profile set up locally, to provide a userspace to run in?

Trackbacks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>