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.
thanks fout this script, but this not work in powershell version 1,
can you refix for powershell version 1.
Why do you want to run it with powershell V1 when the entire world is running with V -ge 2
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?
Sam, thanks for highlighting that. I corrected the code to return the tasks at root level as well.
don’t work correctly!
What is the error you are seeing?
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
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.
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
http://techibee.com/contact-us — you can get my contact details from here. I hope you are not referring any third party modules.
this comment is the only mention I can find on the internet of a “-computername” parameter for get-scheduledtask.
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.
Try this.
PS:\>c:\temp\Get-ScheduledTasks.ps1 -ComputerName Comp1 | Out-File c:\temp\tasks.txt
Your script as is doesn’t work for remote servers. This line:
$SchService.Connect()
should instead be:
$SchService.Connect($Computer)
Thanks for correcting. I missed that earlier. Now the code is modified to reflect the correction.
Hello Sitaram
Thanks for the script! But how can I use the command “$SchService.Connect($Computer)” with other credentials?
Thanks and Regards,
Paul
Works great, thank you. For those that are having issues make sure that you launch PS as Administrator.
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?
AFAIK, you need to get the XML file, modify the way you want, and then import back into task scheduler.
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
Get-ScheduledTasks.ps1 -ComputerName (Get-Content c:\Servers.txt)
Populate servers.txt with the name of the computers you want to query.
Hi Thanks for providing the script. it works great.
how to get the last run results of the task?
regards
Esu
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
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%%”
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*” }
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.
Can you give an example of what you mean by “Task to run”?
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
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.
Is there an element to add to this script to get the RunAsUser of each task?
Hi,
you need to parse the task xml file to get that information.
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?
It is possible. Watch out for new posts on this blog.
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
I just wanted to say thank you for such a great script. It is excellent.