Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
What's with the backslashes in there? I'm fairly sure that's not what schtasks expects there, but rather a forward slash. –  Joey Jun 18 '10 at 21:37
    
ha! you're right. I've corrected it above –  jdiaz Jun 20 '10 at 4:20

5 Answers 5

up vote 6 down vote accepted

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

share|improve this answer
    
This works great if you're using Win2k8 boxes (or vista/W7). Unfortunately, it doesn't work with W2k3 servers (which are still very common in my environment). –  Mike Shepard Jun 28 '10 at 3:09
    
@MikeShepard If you want the new toys, you have to stop using 11 year old operating systems. –  Ryan Ries Jan 30 at 0:30
    
That's the story of my career. :-( –  Mike Shepard Jan 30 at 13:08

Here's a blog post I wrote about doing this. Essentially, I took the output of the /FO LIST /V, wrote that to a file, and imported it back in as objects using import-csv

share|improve this answer
1  
You're on the right track but writing to a temporary file is unnecessary here: schtasks /query /fo csv /v|convertfrom-csv works just fine –  Joey Jun 18 '10 at 21:39
    
this is neat but still not easily queryable –  jdiaz Jun 20 '10 at 4:19
    
Johannes: you're right, but I really (really) dislike properties that have embedded spaces/colons/slashes. jdiaz: What's not queryable? The script I posted and Johannes' revision both return native powershell objects with properties. They should be just as queryable as any other powershell entities. –  Mike Shepard Jun 28 '10 at 3:08
    
Truly - they are queryable...schtasks /query /fo csv /v /s "myserver" | convertfrom-csv | Select TaskName, "Last Run Time", Author | ? {$_.Author -notmatch "microsoft|N/A|Author"} will provide you with all the custom scheduled tasks you have. –  SliverNinja Jan 29 at 22:56

if you don't need to do it in powershell then the following will work

schtasks /query | findstr /i "mytask"

ps version
schtasks /query | ?{$_ -like 'mytask'}

share|improve this answer
    
any full source code sample for create, query and delete tasks using PS ? –  Kiquenet Sep 10 '12 at 9:01

You are overthinking it.

Commandline for what you want schtasks /query /s %computername%|FIND /I "%name_of_task%"

example schtasks /query /s server01|FIND /I "schedule"

share|improve this answer

You may try:

schtasks /query /FO CSV | ConvertFrom-CSV | Where { $_.TaskName -eq "myTask" } 

The trick is in converting output to CSV first, and then back to a powershell object.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.