Powershell – Query schtasks using Powershell

powershellscheduled-taskwindows 7

On a Windows7 machine I am trying I can run a query to view all the scheduled tasks using schtasks.exe

This is fine but I would also like to filter the result set using something like

schtasks /query | where { $_.TaskName -eq "myTask" } 

The problem is I don't this schtasks returns a properly formatted list for the where function to work.

I've also tried:

schtasks /query /FO LIST
schtasks /query | format-list | where ....

those don't work either.

What would be the best way to query the schtasks on a local computer using Win7 and be able to filter them

Best Answer

You could try to use schtasks, which will leave you parsing text. This is almost always error prone, and definitely more difficult than it is to take the output of a command.

There happens to be a TaskScheduler module in the PowerShellPack. Once you install the PowerShell pack, to get all of the scheduled tasks, use:

Import-Module TaskScheduler
Get-ScheduledTask -Recurse

Since these are real objects, to find a task of a particular name, you can use:

Get-ScheduledTask -Recurse |  Where-Object { $_.Name -like "*Task*"}

In general, you will find that the PowerShell community has taken a lot of harder to use command lines, like schtasks, and turned them into easy-to-use cmdlets, like Get-ScheduledTask.

See Also:

Sending Automated Emails using the TaskScheduler Module

Hope this helps