none
SCHTASKS in Windows 7 RRS feed

  • Question

  • Sorry if I'm in the wrong forum, but -

    I have a couple of questions related to Schtasks in Windows 7.  I am currently in the process of creating a Windows 7 pilot that will be used by over 300 different locations with two possible types of users, depending on the location.  We have a batch script that sometimes needs to run to create our own schtasks depending on which of the two users they are, so if one user logs on and then at another time the other type of user logs on, with our current Windows XP systems, all Schtasks are automatically deleted and new schtasks are created for the other type of user.

    However, with Windows 7, I see it comes loaded with quite a few schtasks already, such as the automatic defrag.  Our batch script used the simple command:

    Schtasks /Delete /TN * /F

    and was then followed by our Create statements for the new tasks.  However, it seems that the wildcard (*) only works for deleting all tasks.  For example, I can't have it delete only schtasks with "AUTO" at the beginning of the task name, such as

    Schtasks /Delete /TN AUTO* /F

    Does anyone know of a way to do this?  Using just the wildcard will blow out all schtasks including the ones already loaded on Windows 7 which we would like to keep.

    My second question.  Is there a Schtasks statement that will show me the "CREATE" statement for a Schtask?  For example, if I show a schtask named AUTOLOADDB and want to see the Create statement that was used so I know what file is being called and how often, is there a way for me to see the original CREATE statement?

    Thank you

    Wednesday, July 18, 2012 5:50 PM

All replies

  • schtasks /query /v /fo:list

    --
    .
    --
    "BiggRedd" wrote in message news:f9900602-9b4a-4a19-b0f4-d14a66627deb...

    Sorry if I'm in the wrong forum, but -

    I have a couple of questions related to Schtasks in Windows 7.  I am currently in the process of creating a Windows 7 pilot that will be used by over 300 different locations with two possible types of users, depending on the location.  We have a batch script that sometimes needs to run to create our own schtasks depending on which of the two users they are, so if one user logs on and then at another time the other type of user logs on, with our current Windows XP systems, all Schtasks are automatically deleted and new schtasks are created for the other type of user.

    However, with Windows 7, I see it comes loaded with quite a few schtasks already, such as the automatic defrag.  Our batch script used the simple command:

    Schtasks /Delete /TN * /F

    and was then followed by our Create statements for the new tasks.  However, it seems that the wildcard (*) only works for deleting all tasks.  For example, I can't have it delete only schtasks with "AUTO" at the beginning of the task name, such as

    Schtasks /Delete /TN AUTO* /F

    Does anyone know of a way to do this?  Using just the wildcard will blow out all schtasks including the ones already loaded on Windows 7 which we would like to keep.

    My second question.  Is there a Schtasks statement that will show me the "CREATE" statement for a Schtask?  For example, if I show a schtask named AUTOLOADDB and want to see the Create statement that was used so I know what file is being called and how often, is there a way for me to see the original CREATE statement?

    Thank you

    Wednesday, July 18, 2012 9:08 PM
  • Thank you.  That does help with all tasks that MS has already put on the system.

    But do you know of any way to do a wild card search for task names?  All of our task names have the user name as the first part of their task name and it would make things MUCH less complicated if I could delete only the things starting with our user names so that new tasks can be created each time this script is run without deleting the 18 tasks that Microsoft already has on the system, rather than trying to come up with all the Create statements to add to my batch script to keep those MS tasks.

    Eric

    Monday, July 23, 2012 8:19 PM
  • Try using vbs rather than batch file. From the Platform SDK.

    Displaying Task Names and States (Scripting)

    This scripting example shows how to enumerate tasks in a task folder and display property values from each task.

    The following procedure describes how to display task names and states for all the tasks in a task folder.

    To display task names and state for all the tasks in a task folder

    1. Create the TaskService object.

      This object allows you to connect to the Task Scheduler service and access a specific task folder.

    2. Get a task folder that holds the tasks you want information about.

      Use the TaskService.GetFolder method to get the folder.

    3. Get the collection of tasks from the folder.

      Use the TaskFolder.GetTasks method to get the collection of tasks (RegisteredTaskCollection).

    4. Get the number of tasks in the collection and enumerate through each task in the collection.

      Use the RegisteredTaskCollection collection of objects to get a RegisteredTask object instance. Each instance will contain a task in the collection. You can then display the information (property values) from each registered task.

    The following VBScript example shows how to enumerate through a collection of registered tasks in the root task folder and display the name and state for each task.

    '---------------------------------------------------------
    ' This sample enumerates through the tasks on the local computer and
    ' displays their name and state.
    '---------------------------------------------------------
    
    
    ' Create the TaskService object.
    Set service = CreateObject("Schedule.Service")
    call service.Connect()
    
    ' Get the task folder that contains the tasks. 
    Dim rootFolder
    Set rootFolder = service.GetFolder("\")
     
    Dim taskCollection
    Set taskCollection = rootFolder.GetTasks(0)
    
    Dim numberOfTasks
    numberOfTasks = taskCollection.Count
    
    If numberOfTasks = 0 Then 
        Wscript.Echo "No tasks are registered."
    Else
        WScript.Echo "Number of tasks registered: " & numberOfTasks
        
        Dim registeredTask
        For Each registeredTask In taskCollection
            WScript.Echo "Task Name: " & registeredTask.Name
        
            Dim taskState 
            Select Case registeredTask.State 
                Case "0"
                    taskState = "Unknown"
                Case "1"
                    taskState = "Disabled"
                Case "2"
                    taskState = "Queued"
                Case "3"
                    taskState = "Ready"
                Case "4"
                    taskState = "Running"
            End Select
    
            WScript.Echo "    Task State: " & taskState
        Next
    End If
    
    

    --
    .
    --
    "BiggRedd" wrote in message news:9c397ff6-2bb1-4d69-a781-5fbdc9909776...

    Thank you.  That does help with all tasks that MS has already put on the system.

    But do you know of any way to do a wild card search for task names?  All of our task names have the user name as the first part of their task name and it would make things MUCH less complicated if I could delete only the things starting with our user names so that new tasks can be created each time this script is run without deleting the 18 tasks that Microsoft already has on the system, rather than trying to come up with all the Create statements to add to my batch script to keep those MS tasks.

    Eric

    Tuesday, July 24, 2012 8:32 AM
  • This code deletes tasks that start with the users username
     
    Set service = CreateObject("Schedule.Service")
    call service.Connect()
     
    ' Get the task folder that contains the tasks.
    Dim rootFolder
    Set rootFolder = service.GetFolder("\")
     
    Dim taskCollection
    Set taskCollection = rootFolder.GetTasks(0)
     
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set WshEnv= WshShell.Environment("Process")
     
    Username = WshEnv("USERNAME")
     
    Dim numberOfTasks
    numberOfTasks = taskCollection.Count
     
    If numberOfTasks = 0 Then
        Wscript.Echo "No tasks are registered."
    Else
        WScript.Echo "Number of tasks registered: " & numberOfTasks
       
        Dim registeredTask
        For Each registeredTask In taskCollection
            If Left(registeredTask.Name,Len(username)) = Username then
          RootFolder.DeleteTask registeredTask.Name, 0
     End If
        Next
    End If

    --
    .
    --
    "BiggRedd" wrote in message news:9c397ff6-2bb1-4d69-a781-5fbdc9909776...

    Thank you.  That does help with all tasks that MS has already put on the system.

    But do you know of any way to do a wild card search for task names?  All of our task names have the user name as the first part of their task name and it would make things MUCH less complicated if I could delete only the things starting with our user names so that new tasks can be created each time this script is run without deleting the 18 tasks that Microsoft already has on the system, rather than trying to come up with all the Create statements to add to my batch script to keep those MS tasks.

    Eric

    Tuesday, July 24, 2012 11:04 AM
  • Here's an all batch option. Remember the for variable needs to have double leading % in a CMD/BAT file:

    for /F "usebackq tokens=2 delims=," %t in (`schtasks /query  /v /fo:csv ^| findstr /I /C:"\\SF\\"`) do schtasks /delete /tn %t

    I created a new folder in the Tasks pane called SF and created tasks within it. This will remove any tasks in that tree.

    The findstr can be modified to search for any task pattern you desire.

    Tom


    • Edited by TomJenkins1 Thursday, August 22, 2013 2:14 PM
    • Proposed as answer by TomJenkins1 Thursday, August 22, 2013 2:14 PM
    Thursday, August 22, 2013 1:18 PM