{"id":8331,"date":"2012-08-11T00:01:00","date_gmt":"2012-08-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/08\/11\/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script\/"},"modified":"2012-08-11T00:01:00","modified_gmt":"2012-08-11T00:01:00","slug":"weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script\/","title":{"rendered":"Weekend Scripter: Use the Windows Task Scheduler to Run a Windows PowerShell Script"},"content":{"rendered":"<p><b>Summary<\/b>: Create a scheduled job to run a Windows PowerShell script that creates an HTML Server uptime report<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The amount of emails that are generated when attempting to organize and to arrange an event such as <a href=\"http:\/\/powershellsaturday.com\/002\/\" target=\"_blank\">PowerShell Saturday<\/a> is amazing. Every day, it seems, there are nearly a dozen email messages about one detail or another. Windows PowerShell Microsoft MVP, Jim Christopher, is doing an excellent job of keeping things on track, and the Scripting Wife and Brian Wilhite have been hanging right in there with the work. The event truly will be a major big deal, and it will be fun as well as educational. It will be worth the time to register and to attend this event if you are in the Charlotte, North Carolina area on September 15, 2012.<\/p>\n<h2>Creating a Scheduled task to run a PowerShell script<\/h2>\n<p>The first thing I need to create a scheduled task to run a Windows PowerShell script is the command line that I will execute. The easy way to find this is to use the <b>Run<\/b><i> <\/i>command. At times, I need to know what the command-line switches are for PowerShell.exe. To see these, I open Windows PowerShell and type <i>powershell \/?<\/i> and then I examine the output that displays. The command and the output from the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2110.HSG-8-11-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2110.HSG-8-11-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>When I know which switches to use, I practice my command via <b>Run<\/b>. The following image illustrates using <b>Run<\/b> to launch Windows PowerShell and to run a Windows PowerShell script. Keep in mind that this will open, and close Windows PowerShell, which is fine for a script producing a report. In testing, I often use the <b>&ndash;noexit<\/b> switch to see any errors arising from the operation.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7608.HSG-8-11-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7608.HSG-8-11-12-02.png\" alt=\"Image of message\" title=\"Image of message\" \/><\/a><\/p>\n<p>When I know the command line, I use the Task Scheduler tool, and create a new basic task. First, I need to assign a name and a description. I find it useful to provide a good description as well as a decent name because it facilitates performing maintenance on the task.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8130.HSG-8-11-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8130.HSG-8-11-12-03.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>The next pane is the Task Trigger pane. It is pretty basic, and self-explanatory. Because I want to create a daily task, I leave that selected. After it is created, it is easy to edit the scheduled task to make it run the task more often, such as every hour if that is the need. One reason I use the Basic Task Wizard is that it is easy to get through the steps needed to create the basic task. I always edit stuff later. The Task Trigger pane is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1050.HSG-8-11-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1050.HSG-8-11-12-04.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Now it is time to set the schedule for the task. In this example, the task runs every morning at 7:00 AM beginning on August 11, 2012.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5810.HSG-8-11-12-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5810.HSG-8-11-12-05.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>In the Action pane that follows, I select that we want the scheduled task to <b>Start a program<\/b>, and then click <b>Next<\/b>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5672.HSG-8-11-12-06.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5672.HSG-8-11-12-06.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>In the <b>Start a Program<\/b> pane, I cheat by placing the command I tested previously from the <b>Run<\/b> box into the <b>Program\/script<\/b> box. I then click <b>Next<\/b>. The Start a Program pane is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6204.HSG-8-11-12-07.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6204.HSG-8-11-12-07.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Here is where the <i>cheating <\/i>part comes in to play. I used, directly, the command I tested in the <b>Run<\/b> box for my program. Rather than attempting to break things up, I simply copied the entire line. The Scheduled Task Wizard is smart enough to know what I wanted to do. It prompts, but it knows. The prompt appears here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7635.HSG-8-11-12-08.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7635.HSG-8-11-12-08.png\" alt=\"Image of message\" title=\"Image of message\" \/><\/a><\/p>\n<p>When I have completed the Create Basic Task Wizard, I want to open the task and make a couple of additional changes. The easy way to do this is to select the <b>Open the Properties dialog for this task when I click Finish<\/b>, as shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5315.HSG-8-11-12-09.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5315.HSG-8-11-12-09.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Because the task runs on a server, and because one might not be logged on to the server at the time the task is to run, it makes sense to tell the task to run whether or not the user is logged on. This opens a credential dialog, and allows me to set the password for the task. This option appears on the <b>General<\/b> tab of the scheduled job as shown in the image here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3730.HSG-8-11-12-10.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3730.HSG-8-11-12-10.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>When I have completed configuring the scheduled task, I always right-click the job and select <b>Run<\/b><i>. <\/i>Then I examine the job history to ensure that the task completed properly. The <b>History<\/b> tab of the scheduled job is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4274.HSG-8-11-12-11.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4274.HSG-8-11-12-11.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Well, that is about all there is to creating a scheduled job to run a Windows PowerShell script. In Windows&nbsp;8 and Windows Server&nbsp;2012, there are Windows PowerShell cmdlets to create the scheduled job and to create the job triggers and actions&mdash;but that will be the subject of a later Hey, Scripting Guy! Blog post.<\/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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Create a scheduled job to run a Windows PowerShell script that creates an HTML Server uptime report Microsoft Scripting Guy, Ed Wilson, is here. The amount of emails that are generated when attempting to organize and to arrange an event such as PowerShell Saturday is amazing. Every day, it seems, there are nearly a [&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,61,45],"class_list":["post-8331","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scheduled-tasks","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Create a scheduled job to run a Windows PowerShell script that creates an HTML Server uptime report Microsoft Scripting Guy, Ed Wilson, is here. The amount of emails that are generated when attempting to organize and to arrange an event such as PowerShell Saturday is amazing. Every day, it seems, there are nearly a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8331","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=8331"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8331\/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=8331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}