Dec 3 2007

How to schedule a Windows Powershell script

If you're new here, you may want to start with my most popular posts. Then, subscribe to my RSS feed to stay updated. Thanks for visiting!

Google Query: schedule a powershell script

I was recently working on a project where I needed to schedule a Windows Powershell script to run. After some searching, I came across a post that lead me in the right direction at Mutable.net. Here is how you can schedule a Windows Powershell Script:

The first thing you need to do is make sure that Powershell is set to execute Powershell scripts, instead of only allowing interactive commands to be run in the Powershell environment.

Type the following at the Powershell command prompt:
set-executionpolicy RemoteSigned

This will allow the system to run Powershell scripts that are created locally (Remote Powershell scripts that may be downloaded must be signed).

Once this is done, you can create your Powershell script using notepad. Just make sure you name the file with an extension of .ps1 . Now to run the script outside of its Powershell environment you type a command similar to the following:
powershell -command "& 'MyScript.ps1' "

Just put the above command into a .bat or .cmd file and schedule it like you would normally schedule a script to be run with Windows task scheduler. Happy coding!

Technorati Tags: , ,

Related posts:

  1. How to send an email using a Windows Powershell script
  2. How to deal with long filenames in Windows Powershell
  3. How to put line breaks into a text string in Windows Powershell

TAGS:

5 Comments on this post

Trackbacks

  1. Roland Serman » Blog Archive » MARS 08/08 wrote:

    [...] I take no credit for this, I found it at SearchMarked.com posted by hstagner. [...]

    August 26th, 2008 at 10:08 am
  1. Ivan Versluis said:

    Thanks for this post. I created small powershell script to read a text file and send the text lines to a smtp server.

    Right now a scheduled task with a cmd script is created to run the ps1 file each day.

    December 15th, 2007 at 2:17 pm
  2. hstagner said:

    Hello Ivan,

    I am glad this example could be incorporated into one of your scripts.

    Thanks for reading!

    Regards,

    Harley Stagner

    December 17th, 2007 at 11:01 am
  3. Shaurav Garg said:

    i used your above mentioned trick to schedule a powershell script. Although the script runs as schedule, its output is different from when i run it manually from a powershell window. Do you know why this could be happening?

    June 23rd, 2008 at 7:26 pm
  4. hstagner said:

    Hello Shaurav,

    I am not sure right off the top of my head why that would be happening. Could you send me a copy of the script? Maybe I can figure something out.

    Thank you for reading!

    Regards,

    Harley Stagner

    June 27th, 2008 at 1:00 am

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

Recent Readers

JOIN MY COMMUNITY!
                 
                  Computers Blogs - Blog Top Sites