{"id":74200,"date":"2015-01-15T00:01:00","date_gmt":"2015-01-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/01\/15\/use-powershell-to-create-scheduled-tasks-folders\/"},"modified":"2019-02-18T10:35:59","modified_gmt":"2019-02-18T17:35:59","slug":"use-powershell-to-create-scheduled-tasks-folders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-scheduled-tasks-folders\/","title":{"rendered":"Use PowerShell to Create Scheduled Tasks Folders"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a folder for scheduled tasks.<\/span><\/p>\n<p>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 <a href=\"http:\/\/ignite.microsoft.com\/?linkId=10057588&amp;linkId=11530072#fbid=9j2i0rAgpEL\" target=\"_blank\">Microsoft Ignite Conference in Chicago<\/a>. 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&mdash;they are productive, and they are with absolutely awesome people. Hmmm, I wonder if that is a pattern&#8230;<\/p>\n<p>You know what else is productive and awesome? Using Windows PowerShell to work with scheduled tasks (OK, that was not the greatest &quot;segue.&quot; Word keeps wanting to change that to &quot;Segway.&quot; Oh well.)<\/p>\n<h2>Creating a task folder<\/h2>\n<p>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:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (Get-ScheduledTask).count<\/p>\n<p style=\"margin-left:30px\">180<\/p>\n<p>But trying to find the scheduled tasks can be hit or miss. Here is a view of the tool:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-15-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-15-15-01.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>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 <b>New-ScheduledTaskFolder<\/b> function or cmdlet available. Therefore, I need to revert to using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa383607(v=vs.85).aspx\" target=\"_blank\">Schedule.Service COM API<\/a>. This is actually pretty easy to do. There are four steps involved:<\/p>\n<ol>\n<li>Create the Schedule.Service object.<\/li>\n<li>Connect to the schedule service.<\/li>\n<li>Create a folder object.<\/li>\n<li>Create a new folder.<\/li>\n<\/ol>\n<p>To create an instance of the Schedule.Service object, I use the <b>New-Object<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">$scheduleObject = New-Object -ComObject schedule.service<\/p>\n<p>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:<\/p>\n<p style=\"margin-left:30px\">$scheduleObject.connect()<\/p>\n<p>I need to create a folder object to gain access to the method required to create a new folder. Therefore, I use the <b>GetFolder<\/b><i> <\/i>method from the schedule object I have stored in the <b>$scheduleObject<\/b> variable. I need to retrieve the root folder, and I can use the path <b>&ldquo;\\&rdquo; <\/b>to do this. Because this command returns a <b>TaskFolder<\/b> object, I need a variable to hold the returned object. For this purpose, I use the <b>$rootFolder<\/b> variable. Here is the command:<\/p>\n<p style=\"margin-left:30px\">$rootFolder = $scheduleObject.GetFolder(&quot;\\&quot;)<\/p>\n<p>Now, I can call the <b>CreateFolder<\/b><i> <\/i>method from the <b>TaskFolder<\/b> object. I specify the name of the folder that I want to create. This command is shown here:<\/p>\n<p style=\"margin-left:30px\">$rootFolder.CreateFolder(&quot;PoshTasks&quot;)<\/p>\n<p>When I run the command, I see that the new folder appears off the root. The following image illustrates the results:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-15-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-15-15-02.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>The complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">$scheduleObject = New-Object -ComObject schedule.service<\/p>\n<p style=\"margin-left:30px\">$scheduleObject.connect()<\/p>\n<p style=\"margin-left:30px\">$rootFolder = $scheduleObject.GetFolder(&quot;\\&quot;)<\/p>\n<p style=\"margin-left:30px\">$rootFolder.CreateFolder(&quot;PoshTasks&quot;)<\/p>\n<h2>Deleting a task folder<\/h2>\n<p>If I want to delete a task folder, I call the <b>DeleteFolder<\/b><i> <\/i>method. The <b>DeleteFolder<\/b><i> <\/i>method requires the name of the folder, and I also need to pass a <b>$null<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">$scheduleObject = New-Object -ComObject schedule.service<\/p>\n<p style=\"margin-left:30px\">$scheduleObject.connect()<\/p>\n<p style=\"margin-left:30px\">$rootFolder = $scheduleObject.GetFolder(&quot;\\&quot;)<\/p>\n<p style=\"margin-left:30px\">$rootFolder.DeleteFolder(&quot;poshTasks&quot;,$unll)<\/p>\n<p>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.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,32,3,45],"class_list":["post-74200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scheduled-tasks","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=74200"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74200\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=74200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}