Powershell and schtask with task that has a space

Brad Semrad picture Brad Semrad · May 10, 2012 · Viewed 12k times · Source

I am using schtask command with powershell. The problem that is occuring is that when it is C:\Program Files\ it thinks the path is just C:\Program and the rest of the path is an arguement. I have tried to escape it by using `" pre and post of the field but did make a differnce. How can I accomplish this? I cannot hardcode the path because it can be changed when the user install it.

I was creating this in Windows 7 x64. It creates the task ok the script returns. However, when I view it in the Task Schedular, properties of the task, then actions, and hit edit. It shows the program as C:\Program and then the rest as the argument.

enter image description here

Script:

$folder = Split-Path $MyInvocation.MyCommand.Path -Parent
$app = "\Demon.DatabasePurge.exe"
$exe = $app.Insert(0, $folder)
schtasks /create /tn "Demon Purge Job" /sc daily /st 00:00:00 /tr $exe

Here is what I tried:

$folder = Split-Path $MyInvocation.MyCommand.Path -Parent
$app = "\Demon.DatabasePurge.exe`""
$exe = $app.Insert(0, $folder)
$exe2 = $exe.Insert(0, "`"")
schtasks /create /tn "Demon Purge Job" /sc daily /st 00:00:00 /tr $exe2

Answer

Jon picture Jon · Jun 13, 2013

I was having problems with this as well.. anyone else with this issue the answer is to include single quotes

/TR "'C:\Program Files (x86)\etc\test.exe'"