Testing to see if a scheduled task exist in powershell
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}
Try this one:
Get-ScheduledTask -TaskName "Task Name" -ErrorAction SilentlyContinue -OutVariable taskif (!$task) { # task does not exist, otherwise $task contains the task object}
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