Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell and schtask with task that has a space

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
like image 399
Brad Semrad Avatar asked May 10 '12 21:05

Brad Semrad


People also ask

How do I view tasks in PowerShell?

To retrieve the existing tasks in the task scheduler using PowerShell, we can use the PowerShell command Get-ScheduledTask. We can use the Task Scheduler GUI to retrieve the scheduled tasks. To retrieve using PowerShell, use the Get-ScheduledTask command.

How do I run a PowerShell task in task scheduler?

To start the specific task of the task scheduler using PowerShell, we need to use the Start-ScheduledTask command. When we run the above command, we need to provide the task name.

How do I run an EXE file every 5 minutes?

Go to Control Panel » Administrative Tools » Scheduled Tasks. Create the (basic) task. Go to Schedule » Advanced. Check the box for "Repeat Task" every 10 minutes with a duration of, e.g. 24 hours or Indefinitely.


1 Answers

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'"
like image 165
Jon Avatar answered Sep 22 '22 04:09

Jon