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

No related posts.

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

TAGS:

21 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
  4. Looking for Free or Open Source Software to email new AD users after accounts have been created Drija wrote:

    [...] Save this code as welcome-newuser.ps1. To schedule this task, follow the directions outlined here: http://www.searchmarked.com/windows/how-to-schedule-a-windows-powershell-script.php [...]

    November 16th, 2010 at 4:39 pm
  5. How to Schedule a PowerShell Script « Dmitry’s PowerBlog: PowerShell and beyond wrote:

    [...] a PowerShell Script Published February 3, 2011 PowerShell 3 Comments Despite multiple articles on that in the blogosphere already I keep getting questions on running PowerShell as [...]

    February 3rd, 2011 at 4:03 pm
  6. Looking for Free or Open Source Software to email new AD users after accounts have been created - Admins Goodies wrote:

    [...] Save this code as welcome-newuser.ps1. To schedule this task, follow the directions outlined here: http://www.searchmarked.com/windows/how-to-schedule-a-windows-powershell-script.php [...]

    August 22nd, 2011 at 6:15 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
  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
  8. schnood said:

    Update using Windows 2008:
    This can be done withing the ‘Actions’ tab as described in this technet article:

    http://technet.microsoft.com/en-us/library/ee649304(WS.10).aspx

    7.In the Actions tab:
    a.Click New. The New Action dialog box appears.
    b.In Settings, in Program/Script, type:

    powershell.exe

    c.In Add arguments, type the following, where C:\Users\myLogon\HPCtests.ps1 is the location of the script that you want to run:

    -command “C:\Users\myLogon\HPCtests.ps1”

    d.Click OK.

    April 8th, 2010 at 1:21 pm
  9. Kevin said:

    You can actually schedule a powershell Task with Scheduled tasks and run the very same command in the this article. Thanks for the arrticle and workd beautifully.

    April 21st, 2010 at 11:05 am
  10. Ivan said:

    Thanks for the info.
    I successfully scheduled my first PS script :)

    June 21st, 2010 at 8:27 pm
  11. Steve Electric said:

    It really took MS a while to get a scripted command-line.

    September 24th, 2010 at 5:43 am
  12. Tom said:

    You don’t have to do a batch script to call a power shell script, that is just redundant. all you have to do is scheduale a task to start powershell, with arguments for your .ps1 file IE:

    %systemroot%\system32\WindowsPowerShell\v1.0\powershell.exe “.\$scriptnamehere.ps1″

    for that to work, you need your .ps1 file in the system root of powershell. else, just change your switch to where your .ps1 file is located, i do this to automate snapshots for virtual machines.

    November 10th, 2010 at 11:42 am
  13. Bike Storage said:

    Task with Scheduled tasks and run the very same command in the this article. Thanks for the arrticle and workd beautifully.

    June 8th, 2011 at 1:08 am
  14. Michael said:
  15. Edwin Autman said:

    I realize this can be quite off subject but I am just thinking about starting my very own website and was wanting to know just what is going to be expected create? I am thinking creating a blog site really like the one you have http://www.searchmarked.com/windows/how-to-schedule-a-windows-powershell-script.php would cost you a lot of cash? I am not very internet savvy so I’m not really 100% certain. Any specific suggestions or tips is greatly liked. Thanks alot :) .

    July 24th, 2011 at 4:38 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

Recent Readers

JOIN MY COMMUNITY!
                  Computers Blogs - Blog Top Sites