How can I retrieve the status of a scheduled task using schtasks?

41,320

Solution 1

Unless you really have to use schtasks.exe, use PowerShell with all its superior output and formatting options:

Get-ScheduledTask | ? TaskName -eq GoogleUpdateTaskMachineUA | Select State | ft -AutoSize

or

(Get-ScheduledTask | Where TaskName -eq GoogleUpdateTaskMachineUA ).State

Solution 2

Try schtasks /query with /v option for verbose output and /fo list for list format. Then filter results with find command.

schtasks /query /tn "GoogleUpdateTaskMachineUA" /v /fo list | find "Status:"

You get string like this

Status:         Running

To extract value of status use for command:

for /f "delims=: tokens=2" %a in ('schtasks /query /tn "GoogleUpdateTaskMachineUA" /v /fo list ^| find "Status:"') do @echo %a
  • To use in batch file replace %a with %%a
  • To escape symbols like | or > in for command use ^

Solution 3

Is there any way to retrieve Status only?

Use the following command:

for /f "usebackq skip=3 tokens=4" %a in (`schtasks.exe /query /tn "GoogleUpdateTaskMachineUA"`) do @echo %a

Notes:

  • skip=3 - ignore the header lines
  • tokens=4 - grab the 4th token (the value in the status column)
  • To use in a batch file, replace %a with %%a

Example usage:

F:\test>schtasks.exe /query  /tn "GoogleUpdateTaskMachineUA"

Folder: \
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
GoogleUpdateTaskMachineUA                03/02/2016 12:17:00    Ready

F:\test>for /f "usebackq skip=3 tokens=4" %a in (`schtasks.exe /query /tn "GoogleUpdateTaskMachineUA"`) do @echo %a
Ready

Further Reading

Solution 4

I've searched for a good answer to this, and found some things that got me close. What I ended up with is the following.

$taskName="SomeTask"
$serverName="yourserver"
$status = (schtasks.exe /query /tn "$taskName" /s $serverName /v /fo CSV | ConvertFrom-Csv | Select-Object -Property "Status").Status

If you want all of the items from the task, you can do something similar to the following...

$task= schtasks.exe /query /tn "$taskName" /s $serverName  /v /fo CSV | ConvertFrom-Csv | Select-Object

Then you can select whichever property you want to work with...

$task.Status
$task."Last Result"

Remember to use quotes around the properties with spaces in them!

To clarify, my response is using Powershell when Get-ScheduledTask is not available, which it wouldn't be if running Windows 7, or Windows Server 2008. While one answer does provide a way to get the status, I feel like this method is easier to understand and work with. Even more so if someone might be interested in getting other properties of the scheduled task without having to parse which column the property happens to be in. In short, my answer is the solution that I was looking for, so I thought I would share it for other like minded individuals.

Share:
41,320

Related videos on Youtube

Oskar K.
Author by

Oskar K.

Updated on September 18, 2022

Comments

  • Oskar K.
    Oskar K. over 1 year

    When I run this:

    schtasks.exe /query  /tn "GoogleUpdateTaskMachineUA"
    

    I get this:

    Folder: \
    TaskName                                 Next Run Time          Status
    ======================================== ====================== ===============
    GoogleUpdateTaskMachineUA                N/A                    Disabled
    

    Is there any way to retrieve Status only?

  • Sandeep
    Sandeep almost 6 years
    Tried this. It works good only for task without space in name. When there are spaces in task name, this does not work. Not yet tried, but It may work if I try to adjust tokens values by counting spaces in output.
  • DavidPostill
    DavidPostill almost 6 years
    @Sandeep Hmm. Tricky. There are some possible workaround in Windows Batch file count numbers of tokens - Stack Overflow
  • Scott - Слава Україні
    Scott - Слава Україні over 5 years
    (1) This is PowerShell, right?  When the question (and many of the answers) refer to CMD, it’s nice to mention explicitly that your answer uses PowerShell.  (2) There are a couple of PowerShell answers already; one that uses Get-ScheduledTask and one that uses (schtasks /query …) -split.  How is your answer better than those?    Please do not respond in comments; edit your answer to make it clearer and more complete.
  • wp78de
    wp78de about 5 years
    Thank you, this works. Simply adjust the number of tokens if your scheduled task has spaces, e.g. for /f "usebackq skip=3 tokens=7" %a in (`schtasks.exe /query /tn "Refresh Dashboard Cache"`) do @echo %a
  • Stephan
    Stephan over 4 years
  • Alexandru Dicu
    Alexandru Dicu over 2 years
    This does not work on Windows 7.