January 15th, 2015

Use PowerShell to Create Scheduled Tasks Folders

Doctor Scripto
Scripter

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a folder for scheduled tasks.

Microsoft Scripting Guy, Ed Wilson, is here. Today I have several meetings, and I am really looking forward to them. One of my meetings is with the Windows PowerShell team. I have a meeting with them every other week, and it is always a lot of fun. They are great people! My other meeting is with the team working with the Microsoft Ignite Conference in Chicago. That is probably all I can say right now. But suffice it to say, that I get to attend fun and exciting meetings. That is why I, as opposed to some people I know, love to have meetings—they are productive, and they are with absolutely awesome people. Hmmm, I wonder if that is a pattern…

You know what else is productive and awesome? Using Windows PowerShell to work with scheduled tasks (OK, that was not the greatest "segue." Word keeps wanting to change that to "Segway." Oh well.)

Creating a task folder

When I look at the Task Scheduler tool, I see lots of folders that contain hundreds of scheduled tasks. On my computer, it is 180 scheduled tasks as shown here:

PS C:\> (Get-ScheduledTask).count

180

But trying to find the scheduled tasks can be hit or miss. Here is a view of the tool:

Image of menu

A better way to work with the scheduled tasks I create, is to create my own folders that I can use to group them together. The problem is that there is no New-ScheduledTaskFolder function or cmdlet available. Therefore, I need to revert to using the Schedule.Service COM API. This is actually pretty easy to do. There are four steps involved:

  1. Create the Schedule.Service object.
  2. Connect to the schedule service.
  3. Create a folder object.
  4. Create a new folder.

To create an instance of the Schedule.Service object, I use the New-Object cmdlet, and I specify that I want to create a new COM object. I use a variable to store the returned object. This is shown here:

$scheduleObject = New-Object -ComObject schedule.service

Now that I have an instance of the Schedule.Service object, I connect to the Task Scheduler service. Because no object returns from this command, I do not need another variable. Here is the command:

$scheduleObject.connect()

I need to create a folder object to gain access to the method required to create a new folder. Therefore, I use the GetFolder method from the schedule object I have stored in the $scheduleObject variable. I need to retrieve the root folder, and I can use the path “\” to do this. Because this command returns a TaskFolder object, I need a variable to hold the returned object. For this purpose, I use the $rootFolder variable. Here is the command:

$rootFolder = $scheduleObject.GetFolder("\")

Now, I can call the CreateFolder method from the TaskFolder object. I specify the name of the folder that I want to create. This command is shown here:

$rootFolder.CreateFolder("PoshTasks")

When I run the command, I see that the new folder appears off the root. The following image illustrates the results:

Image of menu

The complete script is shown here:

$scheduleObject = New-Object -ComObject schedule.service

$scheduleObject.connect()

$rootFolder = $scheduleObject.GetFolder("\")

$rootFolder.CreateFolder("PoshTasks")

Deleting a task folder

If I want to delete a task folder, I call the DeleteFolder method. The DeleteFolder method requires the name of the folder, and I also need to pass a $null as the second parameter. Other than that, the code is exactly the same as that I used to create a folder. Here is a script to delete the folder I just created:

$scheduleObject = New-Object -ComObject schedule.service

$scheduleObject.connect()

$rootFolder = $scheduleObject.GetFolder("\")

$rootFolder.DeleteFolder("poshTasks",$unll)

That is all there is to using Windows PowerShell to create and delete folders for scheduled tasks. Scheduled Task Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.