You can schedule PowerShell scripts through
the Task Scheduler, but it is a little trickier than simple batch
files. If you try to schedule the .ps1 file, Notepad opens the file
instead of the script running.
As an example, imagine that you created the following script and named it c:\scripts\feedback.ps1:
$obj = new-object -comobject wscript.shell
$intButton = $obj.Popup("Your message",0,"Title", 0)
Tip
This script displays a popup dialog box similar to Figure 1,
though you can change the title and message to anything desired. You
can test your script from the command prompt (not the PowerShell
prompt) by entering the following: powershell c:\scripts\feedback.ps1.
You can use the following steps to schedule it with Task Scheduler.
Steps | Comments |
---|
1. Launch the Task Scheduler with administrative permissions. | Click Start, type Schedule in the Start Search text box, right-click Task Scheduler, and then select Run As Administrator. |
2. Select Task Scheduler Library. Click Create Basic Task. | The Create Basic Task button is in the right pane. |
3. Name the task Testing and click Next. | This is just for testing but you could create a schedule for a live script and change the name to anything desired. |
4. Select Daily and click Next. | You can also choose to run it weekly, monthly, or any other available timeframe. |
5. Accept the default daily schedule and click Next. | You can modify this time. |
6. Ensure Start a Program is selected and click Next. | This allows you to locate an executable file, including a script. |
7. Enter powershell in the Program/Script text box. Enter c:\scripts\feedback.ps1 in the Add Arguments text box. Click Next. | This has the net effect of typing the following command from the command prompt:
powershell c:\scripts\feedback.ps1.
Your display should look similar to Figure 2. |
8. Click Finish to create the task. | This creates the script. |
9. Select Task Scheduler Library. Locate the Testing task in Task Scheduler. Right-click it and select Run. | The
script runs and the message box displays. However, it doesn’t appear as
the top window so you might have to look for it. Although this isn’t
elegant for a message box, it does show how to make a PowerShell script
run. |
10. Close all the open windows. | If desired, delete the test task you created. |
The previous procedure creates a noninteractive
command-line prompt in the background while the script runs. You can
also run the PowerShell script by creating a batch file to run the
script. The batch file would use the following line:
powershell -noexit c:\scripts\feedback.ps1
The -noexit
switch keeps the PowerShell window open after the script runs, but you
can omit it to ensure that PowerShell closes after the script runs.
Tip
Although it is not a native feature, you
can download a free copy of the specops tool that enables you to
schedule scripts similar to how you can with the Task Scheduler but
with a few more features. You can check it out here: http://technet.microsoft.com/en-us/library/ff730969.aspx.