Apple iTunes
 

In this example, we will use the command-line utility “schtasks.exe” to create a scheduled task.

There are three steps involved:

  1. Create a command string
  2. Execute the command
  3. Determine status (successful or failed)

If running interactively from PowerShell, either of these will work:

example:
schtasks.exe /Create /TN taskname /RU username /SC schedule /TR command-to-run
or
invoke-expression "schtasks.exe /Create /TN taskname /RU username /SC schedule /TR command-to-run"

This is fine because you can see any errors that might occur and respond as needed. However, if running via a PowerShell script, you won’t know whether or not the task was successfully created.

Capturing output to a variable is a step in the right direction….

example (captures text written to stdout):
$result = invoke-expression "schtasks.exe /Create /TN taskname /RU username /SC schedule /TR command-to-run"

But schtasks.exe (like many other programs) writes errors to StdErr which will not be captured to your variable.

There are couple of ways to solve this, one of which is to play with the $error array, parsing its contents for the most recent entry to see if it relates to the command you issued. This seems rather messy and possibly inaccurate since multiple errors from other commands can also be written to the array.

Another way is to invoke the command from a DOS command shell and redirect stderr to stdout and capture both to a PowerShell variable (remember this?:  2>&1 ).

example to capture both stdout and stderr to powershell variable:
$result = invoke-expression "cmd.exe /c `"`"schtasks.exe /Create /TN taskname /RU username /SC schedule /TR command-to-run 2>&1`"`""

Pay careful attention to the quotes in this example (above). Note that cmd.exe needs to have the task scheduler command wrapped in a pair of quotes and you need to escape those quotes using a back-tick character ( ` ).

If your command-to-run through task scheduler has any spaces in it, that too will need to be wrapped in quotes.

example to capture stdout and stderr to powershell variable:

?View Code POWERSHELL
$result = invoke-expression "cmd.exe /c `"`"schtasks.exe /Create /TN taskname /RU username /SC schedule /TR `"command-to-run -param1 -param2`" 2>&1`"`""
"Successful : {0}" -f (!$result -like "*error*")

 

Just remember to use a pair of quotes around the part that is passed to cmd.exe and wrap the program-to-run via the /TR switch in its own separate quotes.

Finally, here is an example showing how to create a scheduled task that will run a PowerShell script upon startup of your computer.

example to create a scheduled task that will run a PowerShell script:

?View Code POWERSHELL
1
2
$result = invoke-expression "cmd.exe /c `"`"schtasks.exe /Create /TN MyTask /RU System /SC ONSTART /TR `"powershell.exe -executionpolicy unrestricted -file c:\scripts\myscript.ps1 -param1 -param2`" 2>&1`"`""
"Successful : {0}" -f (!$result -like "*error*")

 

~~ To see a complete listing of all command-line options for schtasks.exe, open a command window and enter “schtasks /?”  ~~

  2 Responses to “Use PowerShell to create a Scheduled Task”

  1. I’m getting this:

    Invoke-Expression : Ampersand not allowed. The & operator is reserved for future use; use “&” to pass ampersand as a string.

    Wrapping the ampersand in quotes or curly brackets doesn’t seem to help. Am I missing anything?

  2. Hey A,

    You’re doing it right but somehow my posting software munged the powershell code and replaced important characters with HTML replacements, namely the ampersand and greater-than characters. I’ve fixed it now so a simple cut-and-paste from the article should work for you.

    Thanks for catching that!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   
© 2011 Posh Tips Suffusion theme by Sayontan Sinha

Page optimized by WP Minify WordPress Plugin