Alex Rivera | Logout

Powershell and schtask with task that has a space

Asked 2012-05-10T21:20:09.100
17

I am using the schtask command with PowerShell. The problem that occurs is, when the program/script argument contains C:\Program Files\, it thinks the path is just C:\Program and the rest of the path is an argument. I have tried to escape it by using " pre- and post- field, but it did n'tmake a difference. How can I accomplish this? I cannot hard-code the path because it can be changed when the user installs it.

I was creating this in Windows 7 x64. It creates the task OK and the script returns. However, when I view it in the Task Scheduler, 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
Edit
Report

1 Answer

26

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'"
answered 2013-06-13T04:49:41.327

Your Answer