Dec 3 2007

How to schedule a Windows Powershell script

Welcome back!



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: , ,

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

TAGS:

10 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
  2. GPO Backup - Windows 2008 - Jesper Ravn - IT-experts.dk wrote:

    [...] to schedule a Windows Powershell script:http://www.searchmarked.com/windows/how-to-schedule-a-windows-powershell-script.phpRunning Windows PowerShell [...]

    December 29th, 2008 at 5:08 pm
  3. Pause until “then” « The PoSH Student wrote:

    [...] are a lot of suggestions out there on how to use Scheduled Tasks to launch a .ps1 file; basic, wrapped in a .vbs, or even a self-scheduling script. That’s helpful, as long as you want to [...]

    April 22nd, 2009 at 1:08 pm
  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
  5. Gio said:

    Thank You hstagner, you just saved another computer and made a lot of poor people happy (we have no money to have the computer repaired) Many thanks and more power to you!

    November 19th, 2008 at 11:02 pm
  6. ZDING said:

    Following the example, I sceduled a ps1 script on Tidal scheduler to watch IIS web server AppPools, it worked.
    Thanks Hstagner.

    December 10th, 2008 at 3:27 pm
  7. IJB said:

    I accomplished the same thing (server 2008) setting the scheduled task to run powershell.exe with an argument of C:\powershellscript.ps1

    December 3rd, 2009 at 2:55 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

Recent Readers

JOIN MY COMMUNITY!
                  Computers Blogs - Blog Top Sites