Run PowerShell Script using Windows Server 2008 “Task Scheduler”

 

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.

1.      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

2.      I started with a simple PowerShell script file process.ps1 consisting of only one line

  Get-process | Out-FileC:\test\ProcessOutput.txt

3.      You then need to create a .bat file which will invoke the PowerShell script file. Copy the below line into notepad and save as BatchProcess.bat

                powershell.exe C:\Test\process.ps1

4.      Now from the start  menu go to “Administrator Tools” and open Task Scheduler

a.      From The actions menu Click on “Create Task”

1

b.      Under the General Tab, enter task name ProcessTestTask. Ensure that the “Run when user is logged on or not” and “Run with highest privileges” checkboxes are selected.

c.       Ensure that the ‘Configure for’ drop down box is selected for the appropriate operating system.

2

5.   Under the “Trigger” tab, click on “New” and assign a schedule to run the task. Choose times 5 minutes  for testing purposes.

3

6.      Click “Ok”

7.      Under the “Actions” tab click on “New”. Browse to and select the path to the OutputProcesses.bat file, also add the path into the ‘Start in’ text box (it will be your OutputProcesses.bat file folder path).

 4

8.      Click “Ok”.

9.      Leave the other tab settings as default and click OK to save the task settings.

10.   Try to run it manually by right-clicking on the task and selecting run to check if this works.

5

11.    Then try to log off before the task is scheduled to run to confirm that it works when you are logged off.

12.   To find out if this has worked correctly you need to check that the file ProcessOutput.txt has been successfully created in your test directory (“C:\test\”). If this file is successfully created, it means your script works fine.

6

 

7 Responses to “Run PowerShell Script using Windows Server 2008 “Task Scheduler””


  1. 1 John August 9, 2010 at 7:21 pm

    Thanks. That was very helpful and solved my problem.

  2. 2 guillaume October 4, 2010 at 1:14 pm

    Thanks, it is a nice usefull and detailled post !!

  3. 3 PS User November 26, 2010 at 8:51 am

    Perfect, works well and right to the dot

  4. 4 Les March 2, 2011 at 8:55 pm

    Works for an Administrator, but does not output when using other credentials, which are authorized.
    See thread at http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/e298d613-47b8-4492-92d1-0b55cc8497c1

  5. 5 Vicky June 10, 2011 at 9:35 pm

    Thanks, worked like a charm for me.

    Vicky

  6. 6 Dexter June 17, 2011 at 2:42 pm

    This info works fine for a Power shell script, but if like me you need to run a DPM power shell script then this does not work.
    I found that by simply copying path for the DPM management shell shortcut properties and entering it into the batch file to run it first it works:

    C:\Windows\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\Microsoft DPM\DPM\bin\dpmshell.psc1″ -noexit -command “DPM ps1 script path to be ran here”

    Hope this helps someone else.


  1. 1 How to Schedule Database Jobs and Maintenance with SQL Azure « Blog Home for MSSQLDUDE Trackback on March 19, 2011 at 4:09 am

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.