How to schedule PowerShell scripts using Windows Tasks

Hi Folks,

Last week I blogged here about PowerShell v3 and a new feature that is going to do the content of topic automatically however if you don’t have Windows 8/PowerShell 3 and you want to configure a script that you created to run on Windows Server 2008 R2 these following steps can be used:

If your script is not digitally signed, you can run the Set-ExecutionPolicy –ExecutionPolicy Unrestricted as shown in the figure below.

image

Now, it’s time to create a new Windows Task and what really matters for us is the Start a Program section, and these following values must be entered:

  • Program/Script field:
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • Add arguments (optional) field
    -NoLogo -NonInteractive -File C:\path\yourscript.ps1

All pieces together are shown in the picture below.

image

That’s all my friends, from now your PowerShell scripts will work just fine. I do recommend to do a test and depending of the script you may want to configure a specific account to run on the Task and also to configure the script to

Cheers,
Anderson Patricio
http://www.andersonpatricio.ca
http://www.andersonpatricio.org (Portuguese)
Twitter: @apatricio

One thought on “How to schedule PowerShell scripts using Windows Tasks

  1. I’m using this syntax to RUN a scheduled *.ps1 file
    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -command .\automatedemail1.ps1

    AUTOMATEDEMAIL1.PS1
    $email=”?@?.com”
    $subject=”Lunch”
    $body=”back in an hour $(get-date)”

    Send-MailMessage -from $email -to $email -subject $subject -body $body -smtpServer esdsmtp –bodyashtml

    This syntax RUNs a bat file, which in turns executes a ps job
    “C:\Documents and Settings\jyoung\testmail1.bat”

    TESTMAIL1.BAT
    powershell.exe -command .\automatedemail1.ps1

    AUTOMATEDEMAIL1.PS1
    $email=”?@?.com”
    $subject=”Lunch”
    $body=”back in an hour $(get-date)”

    Send-MailMessage -from $email -to $email -subject $subject -body $body -smtpServer esdsmtp –bodyashtml

Leave a Reply

Your email address will not be published. Required fields are marked *