title: Powershell and schtask with task that has a space
body: <p>I am using the <code>schtask</code> command with PowerShell. The problem that occurs is, when the program/script argument contains <code>C:\Program Files\</code>, it thinks the path is just <code>C:\Program</code> and the rest of the path is an argument. I have tried to escape it by using <code>&quot;</code> 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.</p>
  <p>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 <code>edit</code>, It shows the program as C:\Program and then the rest as the argument.</p>
  <p><img src="https://i.stack.imgur.com/nlNhv.png" alt="enter image description here" /></p>
  <p>Script:</p>
  <pre><code>$folder = Split-Path $MyInvocation.MyCommand.Path -Parent
  $app = &quot;\Demon.DatabasePurge.exe&quot;
  $exe = $app.Insert(0, $folder)
  schtasks /create /tn &quot;Demon Purge Job&quot; /sc daily /st 00:00:00 /tr $exe
  </code></pre>
  <p>Here is what I tried:</p>
  <pre><code>$folder = Split-Path $MyInvocation.MyCommand.Path -Parent
  $app = &quot;\Demon.DatabasePurge.exe`&quot;&quot;
  $exe = $app.Insert(0, $folder)
  $exe2 = $exe.Insert(0, &quot;`&quot;&quot;)
  schtasks /create /tn &quot;Demon Purge Job&quot; /sc daily /st 00:00:00 /tr $exe2
  </code></pre>