≡ Menu

Query scheduled tasks from remote computer using Powershell

The below script helps you to query scheduled tasks from remote computer which is running with Windows 7/Windows 2008  or above. You can use this script to query multiple computers to get all the scheduled tasks. You can also query the computer by using a scheduled task name as well.

This script output contains details about when a task is last ran, what is the next execution time as well. These details helps you to quickly look at what all the tasks ran recently and what tasks will run in future at what time. This script also gives the path where this task is present. This useful to identify the location of task in task scheduler folder hierarchy that you you see in GUI.

There are managed APIs like Task Scheduler Managed Wrapper which can give you much more details about scheduled tasks running on a computer. This wrapper has many  other functions like you can create, delete and modify tasks.

 

[cmdletbinding()]                        
param (                        
 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]                        
 [string[]] $ComputerName = $env:computername,                        
 [string] $TaskName                        
)                        
#function to get all scheduled task folder details.                        
function Get-TaskSubFolders {                        
[cmdletbinding()]                        
param (                        
 $FolderRef                        
)                        
$ArrFolders = @()                        
$folders = $folderRef.getfolders(1)                        
if($folders) {                        
 foreach ($folder in $folders) {                        
  $ArrFolders = $ArrFolders + $folder                        
  if($folder.getfolders(1)) {                        
   Get-TaskSubFolders -FolderRef $folder                        
  }                        
 }                        
}                        
return $ArrFolders                        
}                        

#MAIN                        

foreach ($Computer in $ComputerName) {                        
 $SchService = New-Object -ComObject Schedule.Service                        
 $SchService.Connect($Computer)                        
 $Rootfolder = $SchService.GetFolder("\")            
 $folders = @($RootFolder)             
 $folders += Get-Tasksubfolders -FolderRef $RootFolder                        
 foreach($Folder in $folders) {                        
  $Tasks = $folder.gettasks(1)                        
  foreach($Task in $Tasks) {                        
   $OutputObj = New-Object -TypeName PSobject                         
   $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer                        
   $OutputObj | Add-Member -MemberType NoteProperty -Name TaskName -Value $Task.Name                        
   $OutputObj | Add-Member -MemberType NoteProperty -Name TaskFolder -Value $Folder.path                        
   $OutputObj | Add-Member -MemberType NoteProperty -Name IsEnabled -Value $task.enabled                        
   $OutputObj | Add-Member -MemberType NoteProperty -Name LastRunTime -Value $task.LastRunTime                        
   $OutputObj | Add-Member -MemberType NoteProperty -Name NextRunTime -Value $task.NextRunTime                        
   if($TaskName) {                        
    if($Task.Name -eq $TaskName) {                        
     $OutputObj                        
    }                        
   } else {                        
     $OutputObj                        
   }                         
  }                        

 }                        

}

Output:

Feel Free to post here if you have any trouble in using this script. Happy to help!!!

{ 22 comments… add one }
  • liliek August 13, 2012, 12:32 pm

    thanks fout this script, but this not work in powershell version 1,
    can you refix for powershell version 1.

    • Sitaram Pamarthi August 19, 2012, 11:46 pm

      Why do you want to run it with powershell V1 when the entire world is running with V -ge 2

  • sam September 10, 2012, 8:47 pm

    Hello,

    Thank you very much for the script.
    A little detail : tasks at the root folder are not retrieved.

    Can you update the script, please?

    • Sitaram Pamarthi February 10, 2014, 9:38 pm

      Sam, thanks for highlighting that. I corrected the code to return the tasks at root level as well.

  • test February 3, 2014, 8:17 pm

    don’t work correctly!

  • Charlie Mack III February 19, 2014, 10:54 pm

    Just curious to why you didn’t use the get-scheduledtask cmdlet. Using this cmdlet will produce the same results. For ex: Get-scheduledtask -computername “Computer01”. Please correct me if I’m wrong

    • Sitaram Pamarthi February 20, 2014, 9:43 pm

      Hi Charlie, Get-ScheduledTask is available from Windows 2012 which is very new. You can not have it on Windows 2008 servers. Also this post was written when Windows 2012 was not released or in early beta stage. But yes, these cmdlet works when run against windows 2008 server from windows 2012.

      • Charlie Mack III February 20, 2014, 10:46 pm

        Ok, I also want to point out that these cmdlets work against windows 2008 server from windows 7. I currently have a scheduled task, which executes a script continuously. The script sends an html based report to me if any of the scheduled tasks aren’t running or isn’t ready to run and if a task experiences a 0x1 message in the LastTaskResult column. What’s your email? I would like to share the script with you

  • Anderson May 30, 2014, 12:08 am

    Hi Sitaram, i liked your script, it’s exactly the thing that i need, but can i ask you a favour? I need a script just like this, but outputs a log file with all those informations about the tasks instead show in the script window.
    Regards.

    • Sitaram Pamarthi May 30, 2014, 7:32 pm

      Try this.
      PS:\>c:\temp\Get-ScheduledTasks.ps1 -ComputerName Comp1 | Out-File c:\temp\tasks.txt

  • Erick June 26, 2014, 1:36 am

    Your script as is doesn’t work for remote servers. This line:

    $SchService.Connect()

    should instead be:

    $SchService.Connect($Computer)

    • Sitaram Pamarthi July 2, 2014, 12:12 am

      Thanks for correcting. I missed that earlier. Now the code is modified to reflect the correction.

  • Paul January 28, 2015, 1:17 pm

    Hello Sitaram

    Thanks for the script! But how can I use the command “$SchService.Connect($Computer)” with other credentials?

    Thanks and Regards,
    Paul

  • matt March 17, 2015, 7:06 am

    Works great, thank you. For those that are having issues make sure that you launch PS as Administrator.

  • Steven Fried May 29, 2015, 8:54 pm

    This is great. I need to be able to set the command remotely on a scheduled task. I can’t see a way of getting the command from the XML file. Can anyone help?

    • TechiBee June 7, 2015, 7:54 pm

      AFAIK, you need to get the XML file, modify the way you want, and then import back into task scheduler.

  • Brano March 7, 2016, 7:22 pm

    Hi,

    if I want to get scheduler tasks from list of computers ? For example from 300 servers ?
    Is possible to upload list of servers from a file ?

    Thanks
    brano

    • Wintel Rocks March 23, 2016, 11:04 pm

      Get-ScheduledTasks.ps1 -ComputerName (Get-Content c:\Servers.txt)

      Populate servers.txt with the name of the computers you want to query.

  • Esu Shaik July 5, 2016, 9:23 am

    Hi Thanks for providing the script. it works great.

    how to get the last run results of the task?

    regards
    Esu

    • Wintel Rocks September 24, 2016, 10:29 am

      you can try below. Get-ScheduledTask cmdlet available on Windows 10 & Windows 2012 & above versions.

      Get-ScheduledTask | Where State -ne “Disabled” | Get-ScheduledTaskInfo | Select TaskName,TaskPath,LastRunTime, LastTaskResult,NextRunTime,NumberofMissedRuns

Leave a Comment