Query within a Task Scheduler Library folder using schtasks.exe and getting total number of tasks


As we all know that working with task scheduler is not an easy task when it comes to query, change, add etc. through the command prompt.

Here i will share few of the details while working with schtasks.exe.

What is Schtasks.exe ?
Schtasks enables an administrator to create, delete, query, change, run, and end scheduled tasks on a local or remote computer. Running Schtasks.exe without arguments displays the status and next run time for each registered task.

For more information to work with Schtasks follow the link "https://docs.microsoft.com/en-us/windows/desktop/taskschd/schtasks"

Now lets get back to what our topic concerns -  Query within a Task Scheduler Library folder using schtasks.exe and getting total number of tasks.

For Query we just use  - schtasks.exe /query /TN \[FolderName]\

Now to query and get the number of tasks we will follow below process :

Just for an example I'm working for two folder - \Microsoft\Windows\SideShow and \Microsoft\office\

Total count of task in \Microsoft\Windows\SideShow is 4
And in folder \Microsoft\office\ count is 1

I am using skip in for loop which means "number of lines to skip at the beginning of the file".

As when we query for tasks we usually get four lines which are non of the use including Blank line, folder name, titles and separator.
So i have used Skip as 4.

echo off
setlocal enableextensions enabledelayedexpansion
SET /a $count1=0
FOR /f "skip=4"  %%X IN ('schtasks.exe /query /TN \Microsoft\Windows\SideShow\') DO (set /a $count1 += 1)

echo count of \Microsoft\Windows\SideShow\ is !$count1!
 
SET /a $count1=0
FOR /f "skip=4"  %%X IN ('schtasks.exe /query /TN \Microsoft\Office\') DO (set /a $count1 += 1)

echo count of \Microsoft\Office\ is !$count1!

The following line of code will return -
count of \Microsoft\Windows\SideShow\ is 1
count of \Microsoft\office\ is 4


Please let me know if the solution is helpful.


Comments

Popular posts from this blog

Export Data as csv file from SQL server using command line tools

Getting Started with AppCmd.exe