21

I can't figure out why the below code won't work:

Function createFirefoxTask() {
   $schedule = new-object -com Schedule.Service 
   $schedule.connect() 
   $tasks = $schedule.getfolder("\").gettasks(0)

   foreach ($task in ($tasks | select Name)) {
      echo "TASK: $task.name"
      if($task.equals("FirefoxMaint")) {
         write-output "$task already exists"
         break
      }
   }
} 
createFirefoxTask

The output I get is this:

FirefoxMaint                                                                          

TASK: @{Name=FirefoxMaint}.name
TASK: @{Name=Task1}.name
TASK: @{Name=Task2}.name
TASK: @{Name=Task3}.name
TASK: @{Name=Task4}.name
TASK: @{Name=Task5}.name

If I echo $task.name from the shell without going through the script, it properly displays the name.

nullByteMe
  • 6,141
  • 13
  • 62
  • 99

5 Answers5

40

In order to prevent errors with Get-ScheduledTask in case the task doesn't exist - you might want to consider doing :

$taskName = "FireFoxMaint"
$taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName }

if($taskExists) {
   # Do whatever 
} else {
   # Do whatever
}
Jochen van Wylick
  • 5,303
  • 4
  • 42
  • 64
24

Try this one:

Get-ScheduledTask -TaskName "Task Name" -ErrorAction SilentlyContinue -OutVariable task

if (!$task) {
    # task does not exist, otherwise $task contains the task object
}
Distagon
  • 1,018
  • 11
  • 14
  • -OutVariable did not work for me. But just returning the variable did. $task = Get-ScheduledTask -TaskName ... – Action Dan Feb 25 '22 at 01:05
6

When used in a double-quoted string, variable evaluation will stop at punctuation. You can use $() to denote a subexpression within a string, like this:

"TASK: $($task.name)"

PowerShell will then evaluate the expression inside the parentheses and expand the string with the outcome

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
4

If you are using v3.0, then you can do this using Get-ScheduledTask. Forexample,

$task = Get-ScheduledTask -TaskName "FirefoxMaint" -TaskPath \

Then, just need to check the value of $task.

Rick Rainey
  • 11,096
  • 4
  • 30
  • 48
  • 1
    But if I'm running v3.0, it won't do me any good if the clients whose machine I'll run this on are v1.0 right? – nullByteMe Jul 17 '13 at 18:40
  • 2
    In 4.0 it appears that this causes an error when the task does not exist. – emragins Oct 28 '14 at 17:13
  • 1
    @RickRainey is partially correct, the ScheduledTask module uses CIM classes that was only introduced in Windows 8 / Windows Server 2012 http://stackoverflow.com/a/22644879/740575 – sonjz Apr 02 '15 at 22:09
4

You can use the following code to test to see if a scheduled task exists in PowerShell:

if(Get-ScheduledTask FirefoxMaint -ErrorAction Ignore) { #found } else { #not found}
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
panpawel
  • 1,782
  • 1
  • 16
  • 17
  • 3
    You should give more context to an answer rather than just dropping a block of code that the OP or future readers may not understand. – zgue Oct 10 '18 at 20:24
  • It's working. Also you can use variable to store the task - $task = Get-ScheduledTask -TaskName $taskName -ErrorAction Ignore. It works on PowerShell version 5 o the Windows 10. If not use the ErrorAction it will works also but will print an error to the console. – Digiman Oct 18 '19 at 14:49