I would like to have a PowerShell Script to check the task scheduler. If the task name exists then delete it.
if (schtasks /query /tn "mytask") {
schtasks /delete /tn "mytask" /f | Out-Null
}
The syntax works well when the user has the task name in the task scheduler. However, the PowerShell returns the error message when the task name doesn't exist:
schtasks : ERROR: The system cannot find the file specified.
At line:1 char:5
+ if (schtasks /query /tn "mytask") {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: The syst...file specified.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Is there any way to avoid or hide the PS return the error message?