How to make SCHTASKS /Query display the "multiple actions" attribute in query result?

17,053

Solution 1

Batch Script List All Task Scheduler Job Names and Commands

I've provided batch script below that will run from Windows 7 and Windows 10.

Essentially this will:

  1. Run schtasks with the /XML switch and pipe all the XML content it reads through the findstr command with the I switch (case insensitive) filtering it to only redirect the lines that contain the <!-- and <command> strings as ouput to a flat file.
  2. The flat file content is then run through a dynamic PowerShell script that will replace the XML tags with more appropriately formatted field names, trim any leading white space from all lines, remove all blank line, and lastly put a new line before each of the Task Name fields but it'll skip the first line as it won't need to have a line before it—the topmost line in the file.

Note: See the Gotchas section below for potential anomaly detail and items to note.


The Batch Script

@ECHO ON

SET RptFile=%temp%\TaskSchedReport.txt

:: -- This routine sets temp files
SET RptFileTmp=%temp%\~tmpTaskSchedReport.txt
IF EXIST "%RptFileTmp%" DEL /Q /F "%RptFileTmp%"
SET TmpPSScript=%Temp%\~tmpScheduleTasks.ps1
IF EXIST "%TmpPSScript%" DEL /Q /F "%TmpPSScript%"

:SchTask
schtasks /query /XML | Findstr /I "<!-- <command>">"%RptFileTmp%"

:PowerShell
ECHO $origFile = "%RptFileTmp%"                                                    >> "%TmpPSScript%"
ECHO $NewFile = "%RptFile%"                                                        >> "%TmpPSScript%"
ECHO $BlankLine = "`r`n"                                                           >> "%TmpPSScript%"
ECHO (Get-Content $origFile) ^| Foreach-Object {                                   >> "%TmpPSScript%"
ECHO     $_ -replace "<!-- ", 'Task Name (and path): ' -replace "<Command>", 'Command: ' -replace "<[^>]+>", '' -replace '^^\s+', '' -replace '(?m)^^\s*\r?\n', ''>> "%TmpPSScript%"
ECHO     } ^| Set-Content $NewFile                                                 >> "%TmpPSScript%"
ECHO (Get-Content $NewFile) ^| ? {$_.trim() -ne "" } ^| Set-Content $NewFile       >> "%TmpPSScript%"
ECHO (Get-Content $NewFile) ^| Foreach-Object {                                    >> "%TmpPSScript%"
ECHO     $_ -replace "Task Name ", ($BlankLine + "Task Name ") -replace "-->", ''  >> "%TmpPSScript%"
ECHO     } ^| Set-Content $NewFile                                                 >> "%TmpPSScript%"
ECHO (Get-Content $NewFile ^| Select-Object -Skip 1) ^| Set-Content $NewFile       >> "%TmpPSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%TmpPSScript%'"

:: -- Below will open file to view content with the default text editor
explorer.exe "%RptFile%"

Results

Task Name (and path): \Adobe Acrobat Update Task 
Command: C:\Program Files\Common Files\Adobe\ARM\1.0\AdobeARM.exe

Task Name (and path): \GoogleUpdateTaskMachineCore 
Command: C:\Program Files\Google\Update\GoogleUpdate.exe

Task Name (and path): \GoogleUpdateTaskMachineUA 
Command: C:\Program Files\Google\Update\GoogleUpdate.exe

Task Name (and path): \TopSecret 
Command: C:\Folder\CIA.exe
Command: C:\Folder\FBI.exe

Gotchas

If you notice a Task Name field item with no Command field item beneath it, that appears to be due to system scheduled tasks, etc. that have Actions listed as Custom Handler values than cannot be edited see examples and screen shot below.

Example (no commands)

Task Name (and path): \Microsoft\Windows\Shell\WindowsParentalControls 

Task Name (and path): \Microsoft\Windows\Shell\WindowsParentalControlsMigration 

Task Name (and path): \Microsoft\Windows\SideShow\AutoWake 

Task Name (and path): \Microsoft\Windows\SideShow\GadgetManager 

Task Name (and path): \Microsoft\Windows\SideShow\SessionAgent 

Task Name (and path): \Microsoft\Windows\SideShow\SystemDataProviders 

Task Scheduler Job Settings (ones with no commands/actions) enter image description here


Further Resources

Solution 2

Since you say: "I want to still see the task names in relation to what their actions are" in the comment then you can use the below example batch script to get you the expected output with Windows 7.

Essentially this will:

  1. Run the verbose schtasks query command to dump the output to a csv file
  2. With the Windows native PowerShell convertfrom-csv command, it'll convert the csv output to a list like format.
  3. From there it'll use the PowerShell select command with the -property switch to get only the values from the fields you want to see only.
  4. The PowerShell where command with -notcontains comparison operator will filter out extra TaskName objects that for whatever reason dumps to the csv file with the schtasks commands.

Note: You're more limited with what you can and cannot use natively for this task with Windows 7 than you are with newer Windows OSes such as Windows 10.

Batch Script Explicit

You may need to play with the -Width 256 parameter and use larger or smaller int.

@ECHO ON

SET RptFile=C:\Folder\Path\TaskSchedReport.txt
SET TmpPSScript=%Temp%\~tmpScheduleTasks.ps1
IF EXIST "%TmpPSScript%" DEL /Q /F "%TmpPSScript%"

ECHO schtasks /query /v /fo csv ^| ConvertFrom-CSV ^| >> "%TmpPSScript%"
ECHO Select -Property "TaskName","Task To Run" ^|     >> "%TmpPSScript%"
ECHO Where {$_.TaskName -notcontains "TaskName"} ^|   >> "%TmpPSScript%"
ECHO Out-File "%RptFile%" -Width 256                  >> "%TmpPSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%TmpPSScript%'"

:: -- Below will open file to view content with the default text editor
explorer.exe "%RptFile%"

EXIT

Batch Output Snippet

TaskName                                                                                                                         Task To Run                                                                                                                    
--------                                                                                                                         -----------                                                                                                                    
\Adobe Acrobat Update Task                                                                                                       C:\Program Files\Common Files\Adobe\ARM\1.0\AdobeARM.exe                                                                       
\Adobe Acrobat Update Task                                                                                                       C:\Program Files\Common Files\Adobe\ARM\1.0\AdobeARM.exe                                                                       
\GoogleUpdateTaskMachineCore                                                                                                     C:\Program Files\Google\Update\GoogleUpdate.exe /c                                                                             
\GoogleUpdateTaskMachineCore                                                                                                     C:\Program Files\Google\Update\GoogleUpdate.exe /c                                                                             
\GoogleUpdateTaskMachineUA                                                                                                       C:\Program Files\Google\Update\GoogleUpdate.exe /ua /installsource scheduler                                                   
\Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan                                                            c:\Program Files\Microsoft Security Client\\MpCmdRun.exe Scan -ScheduleJob -RestrictPrivileges                                 
\Microsoft\Windows\Active Directory Rights Management Services Client\AD RMS Rights Policy Template Management (Automated)       COM handler                                                                                                                    
\Microsoft\Windows\Active Directory Rights Management Services Client\AD RMS Rights Policy Template Management (Automated)       COM handler                                                                                                                    
\Microsoft\Windows\Active Directory Rights Management Services Client\AD RMS Rights Policy Template Management (Manual)          COM handler                                                                                                                    
\Microsoft\Windows\Autochk\Proxy                                                                                                 %windir%\system32\rundll32.exe /d acproxy.dll,PerformAutochkOperations    

Batch Script Everything

@ECHO ON

SET RptFile=C:\Folder\Path\TaskSchedReport.txt
SET TmpPSScript=%Temp%\~tmpScheduleTasks.ps1
IF EXIST "%TmpPSScript%" DEL /Q /F "%TmpPSScript%"

ECHO schtasks /query /v /fo csv ^| ConvertFrom-CSV ^| >> "%TmpPSScript%"
ECHO Where {$_.TaskName -notcontains "TaskName"} ^|   >> "%TmpPSScript%"
ECHO Out-File "%RptFile%" -Width 256                  >> "%TmpPSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%TmpPSScript%'"

:: -- Below will open file to view content with the default text editor
explorer.exe "%RptFile%"

EXIT

Native PowerShell Explicit

As a bonus I included the straight PowerShell syntax you can use to see the results. Test with and without without the | FL and compare results.

schtasks /query /v /fo csv | ConvertFrom-CSV |
Select -Property "TaskName","Task To Run" |
Where {$_.TaskName -notcontains "TaskName"} | FL

Native PowerShell Everything

schtasks /query /v /fo csv | ConvertFrom-CSV |
Where {$_.TaskName -notcontains "TaskName"} | FL

Further Resources

Share:
17,053

Related videos on Youtube

AK_
Author by

AK_

follow me on twitter | https://twitter.com/i4AK

Updated on September 18, 2022

Comments

  • AK_
    AK_ over 1 year

    I want to display all details about a certain task.

    For example, I have a "MyTask" that has multiple programs to run as actions, shown below: enter image description here

    I want to see the list of programs that the task runs in the command line version of task scheduler (or: schtasks).

    So when I run:

    SCHTASKS /Query /TN "\Microsoft\Windows\MyTask" /FO list /v
    

    But I only get the phrase "multiple actions" instead of the actual actions / program to be run as shown below: enter image description here

    I know I could use the /XML argument to show the list of the programs to be run, but what if I have many tasks to generate results for? I want my output to be a list i.e. /FO list

    How do I solve this?

  • AK_
    AK_ about 7 years
    what a beast answer! I am speechless. Thank you! I'm still getting multiple actions when I run the powershell, I am assuming that's because we are using CSV as output format. Can you change that into XML and see if it works?
  • AK_
    AK_ about 7 years
    When I run: schtasks /query /v /fo csv | ConvertFrom-CSV | Select -Property "TaskName","Task To Run" | Where {$_.TaskName -notcontains "TaskName"} | FL I'm still getting: TaskName : \Microsoft\Windows\MyTask Task To Run : Multiple actions which I suppose is normal because the CSV list doesn't contain as much info as XML. What I was thinking that can be the solution is to implement a 1-liner XML parser that link each "task to run" (even if multiple items) to a "task name" got it?
  • AK_
    AK_ about 7 years
    please do - and thanks again for such helpful detailed answer!
  • AK_
    AK_ about 7 years
    sounds good; as soon as you have a working POC; please add it to the answer by editing it. thank you ALOT!
  • AK_
    AK_ about 7 years
    the beauty of this is beyond words :) thank you!
  • Mark Deven
    Mark Deven over 5 years
    This is not working for me when the paths of the actions have spaces. Example output: Task Name (and path): \PA wake up Command: "C:\Users\IT Command\Documents\Batch Codes\PA\PA.bat" and Task Name (and path): \Example One Command: C:\Users\IT
  • Mark Deven
    Mark Deven over 5 years
    It read the text after the space as an attribute
  • Mark Deven
    Mark Deven over 5 years
    It’s when the action does not have quotes
  • Mark Deven
    Mark Deven over 5 years
    That worked thank you. I did ask this question as well, which relates to my problem: superuser.com/questions/1363277/schtasks-exe-with-quotes
  • Mark Deven
    Mark Deven over 5 years
    Well it was about the error of SchTasks not your answer which does what it is supposed to well.