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.
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
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.
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.
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.
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'"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With