Powershell and schtask with task that has a space
Asked Answered
B

4

16

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
Burlburlap answered 10/5, 2012 at 21:20 Comment(3)
I tried it out using the notepad++ exe under C:\Program Files and your original code worked fine. It created a scheduled task for me.Keyte
Your original code is working for me too with C:\Program Files\SyncToy 2.1\SyncToy.exe. On which OS are you working ?Dyeing
Maybe I wasn't specifically clear. I am able to create the task; however, it will not run. I was using Win7. So I create the task and if you browse the task, right click properties, actions, edit. My task would show the program as C:\Program and then the rest of the path as the argument. I'll update the description.Burlburlap
A
24

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'"
Acquirement answered 13/6, 2013 at 4:49 Comment(0)
D
7

I had the same problem on Windows Server 2008 R2. While Microsoft support indicates you should be able to surround the command with \" I never got that to work, but single quotes work. Moreover, arguments with spaces can also be surrounded in single quotes:

schtasks /create /f /tn "Test" /tr "'c:\program files\test.bat' arg1 'arg 2 with spaces' arg3" /sc Daily /st 00:00

or when building up a string to execute (and using your $folder variable):

$createTask = "schtasks /create /f /tn `"Demon Purge Job`" /tr `"'$folder\Demon.DatabasePurge.exe' arg1 'arg 2 with spaces' arg3`"" /sc daily /st 00:00
Invoke-Expression $createTask

The first example produces the following task action screen: enter image description here

Dijon answered 16/9, 2013 at 8:46 Comment(0)
S
1

To work around this problem, enclose the path portion of the task (not including arguments or switches) between backslash \ and quotation marks " character combinations, for example \". Enclose the complete path of the task (including arguments or switches) between quotation marks as usual when you create a path or command that contains spaces.

Code:

schtasks /create /f /tn "Test" /tr "'c:\program files\test.bat' arg1 'arg 2 with spaces' arg3" /sc Daily /st 00:00

Replace:

schtasks /create /f /tn "Test" /tr "\"c:\program files\test.bat\" arg1 'arg 2 with spaces' arg3" /sc Daily /st 00:00
Strickle answered 22/6, 2018 at 1:57 Comment(0)
Q
1

The command line help already provides the solution:

schtasks /create /?
==> Spaces in file paths can be used by using two sets of quotes, one
    set for CMD.EXE and one for SchTasks.exe.  The outer quotes for CMD
    need to be double quotes; the inner quotes can be single quotes or
    escaped double quotes:
    SCHTASKS /Create
       /tr "'c:\program files\internet explorer\iexplorer.exe'
       \"c:\log data\today.xml\"" ...
Quad answered 28/2, 2023 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.