≡ 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!!!

Comments on this entry are closed.

  • 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

    • Curtiss July 1, 2018, 11:32 am

      this comment is the only mention I can find on the internet of a “-computername” parameter for get-scheduledtask.

  • 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

  • Ramesh February 12, 2017, 6:28 pm

    This is Ramesh. I just used your script and it’s worked as excepted. Thanks for your effort.
    As per your code logic, we should give the exact task name as per task scheduler.Is it possible to get task scheduler with match string not as exact task name ..

    Ex: Get-ScheduledTasks.ps1 -ComputerName (Get-Content c:\Servers.txt) -TaskName “Exact TaskName” instead of this, script can fetch all tasks which are match the string like below
    Get-ScheduledTasks.ps1 -ComputerName (Get-Content c:\Servers.txt) -TaskName “%%TaskName%%”

    • Wintel Rocks February 25, 2017, 11:12 am

      Hi Ramesh,

      It is definitely possible. You just need to make small change to the logic or try something like below.

      Get-ScheduledTasks.ps1 -ComputerName (Get-Content c:\servers.txt) | ? {$_.TaskName -match “*mytask*” }

  • uma July 10, 2017, 3:32 pm

    Hi , how to fetch the “Task to Run” output from the above script. When we try in command prompt with “schtasks.exe /Query /s /FO CSV /V” . it shows “Task to run” but the same not comes in “get-scheduledtask , get-scheduledtaskinfo “. Please help me out on the same.

    • Wintel Rocks July 12, 2017, 8:20 am

      Can you give an example of what you mean by “Task to run”?

  • sumeet March 12, 2018, 7:59 pm

    it cant connect it without admin account it requires local admin permissions do you know a way other than this which doesnt require local account

    • Wintel Rocks March 13, 2018, 9:09 pm

      You want to perform admin tasks without admin rights? Not possible. You need to have admin rights to query most of the system information remotely. No workaround available for this.

  • Scott April 6, 2018, 1:57 am

    Is there an element to add to this script to get the RunAsUser of each task?

    • Wintel Rocks April 9, 2018, 9:57 pm

      Hi,
      you need to parse the task xml file to get that information.

  • swetha May 14, 2019, 4:23 pm

    Hey thanks for your great script.. but then can we have a function added to it that will restart the job from task scheduler of remote server?

    • Wintel Rocks May 18, 2019, 7:19 am

      It is possible. Watch out for new posts on this blog.

  • Priscilla January 6, 2020, 3:59 am

    this is a wonderful script, thank you for making it. I am new to using powershell and was wondering how to make the report come out in one line per scheduled task instead of like the one below. or if not possible, is there a way to just pick up the scheduled tasks I want to focus on. I have over 100 servers and am trying not to have to log on to each after a reboot to check things and this script is exactly what I need as one of my tools to check things after reboot.
    ComputerName : servername
    TaskName : WSTask
    TaskFolder : \Microsoft\Windows\WS
    IsEnabled : True
    LastRunTime : 11/30/1999 12:00:00 AM
    NextRunTime : 12/30/1899 12:00:00 AM

  • RegReg March 12, 2020, 3:09 pm

    I just wanted to say thank you for such a great script. It is excellent.