<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>run powershell script from task scheduler Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/run-powershell-script-from-task-scheduler/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/run-powershell-script-from-task-scheduler/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Tue, 26 Oct 2021 23:21:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>Automate Powershell Scripts With Task Scheduler</title>
		<link>https://thesysadminchannel.com/automate-powershell-scripts-with-task-scheduler/</link>
					<comments>https://thesysadminchannel.com/automate-powershell-scripts-with-task-scheduler/#respond</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Mon, 14 Sep 2020 06:08:44 +0000</pubDate>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Server Administration]]></category>
		<category><![CDATA[Automate Powershell Scripts With Task Scheduler]]></category>
		<category><![CDATA[how to run powershell script using task scheduler]]></category>
		<category><![CDATA[how to use task scheduler]]></category>
		<category><![CDATA[run powershell script from task scheduler]]></category>
		<category><![CDATA[run powershell script using task scheduler]]></category>
		<category><![CDATA[run Powershell scripts as a scheduled task]]></category>
		<category><![CDATA[use task scheduler to run powershell script]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=2458</guid>

					<description><![CDATA[<p>Through out my sysadmin journey I&#8217;ve always been an advocate of implementing as much automation as possible. Whether I automate Powershell Scripts with Task Scheduler or create a function to run interactively in the shell. I believe to the core&#8230; <a href="https://thesysadminchannel.com/automate-powershell-scripts-with-task-scheduler/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/automate-powershell-scripts-with-task-scheduler/">Automate Powershell Scripts With Task Scheduler</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Through out my sysadmin journey I&#8217;ve always been an advocate of implementing as much automation as possible.  Whether I <strong>automate Powershell Scripts with Task Scheduler</strong> or create a function to run interactively in the shell.  I believe to the core that I should always strive to automate myself out of a job.</p>
<p>Today we&#8217;re going to focus on <strong>what is Windows Task Scheduler</strong>, but more importantly <strong>how to run Powershell scripts from Task Scheduler</strong> to automate your tasks.  There are a couple of caveats I&#8217;ve learned over the years but we&#8217;ll get into that later in the article.</p>
<h2>What is Task Scheduler?</h2>
<p>I suppose before we get into the details of how to run Powershell scripts as a scheduled task, it would be a good idea to explain exactly what Task Scheduler is.<br />
&nbsp;</p>
<p>So what is Task Scheduler? <a href="https://en.wikipedia.org/wiki/Windows_Task_Scheduler" rel="noopener noreferrer" target="_blank">Wikipedia explains</a> Task Scheduler as a component of Microsoft Windows that provides the ability to schedule the launch of programs or scripts at pre-defined times or after specified time intervals. </p>
<p>In other words, it&#8217;s a way to schedule jobs or certain tasks at specific time intervals throughout the day, week, month or as a one time basis.</p>
<div id="attachment_2610" style="width: 791px" class="wp-caption aligncenter"><a href="https://thesysadminchannel.com/wp-content/uploads/2020/12/What-is-Task-Scheduler.png" target="_blank" rel="noopener noreferrer"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-2610" src="https://thesysadminchannel.com/wp-content/uploads/2020/12/What-is-Task-Scheduler.png" alt="What is Task Scheduler" width="781" height="468" class="size-full wp-image-2610" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/12/What-is-Task-Scheduler.png?v=1608958454 781w, https://thesysadminchannel.com/wp-content/uploads/2020/12/What-is-Task-Scheduler-125x75.png?v=1608958454 125w, https://thesysadminchannel.com/wp-content/uploads/2020/12/What-is-Task-Scheduler-768x460.png?v=1608958454 768w" sizes="(max-width: 781px) 100vw, 781px" /></a><p id="caption-attachment-2610" class="wp-caption-text">Task Scheduler is available on Windows Server and Windows Client machines. i.e Windows Server 2016/2019 and Windows 10</p></div>
<h2>How To Automate Powershell Scripts with Task Scheduler</h2>
<p>Now that you know what is it, let&#8217;s move on to how you can use it to your advantage. The first step is knowing what we&#8217;ll want to automate (or schedule). In my example I&#8217;m going to automatically email my users once their password is within 10 days of expiration. Luckily, we already have a <a href="https://thesysadminchannel.com/get-password-expiration-date-using-powershell-active-directory/" rel="noopener noreferrer" target="_blank">Get Password Expiration Date</a> Powershell script so now it&#8217;s just a matter setting up a task.  </p>
<p>&nbsp;<br />
Let&#8217;s get to that now.</p>
<ul>
<li>First, we&#8217;ll need to <strong>open Task Scheduler</strong> from the start menu</li>
<li>Choose the path you want to setup the task. I normally choose the root</li>
<li>Right click <strong>Task Scheduler Library</strong> and select <strong>Create Task</strong> (not basic task)</li>
<li>Under the <strong>General tab</strong>, insert the <strong>Name</strong> and <strong>Description</strong></li>
<li>Set the user account (preferably a <strong>service account</strong> with a password that doesn&#8217;t expire)</li>
<li>Check the option to <strong>run whether the user is logged in or not</strong></li>
<li>Configure the dropdown to use the <strong>latest version</strong></li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-General-Tab.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-General-Tab.png" alt="Scheduled Task General Tab - Automate Powershell Scripts" width="629" height="478" class="aligncenter size-full wp-image-2615" /></a><br />
&nbsp;</p>
<ul>
<li>Under the <strong>trigger tab</strong>, select <strong>on a schedule</strong> for time based tasks</li>
<li>Select the time frequency. One time, daily, weekly or monthly</li>
<li>Since we want the task to <strong>run daily</strong>, I&#8217;ll set it <strong>recur every 1 days</strong></li>
<li>Under the <strong>advance settings</strong>, if you want to <strong>run the task every x hours</strong>, enter that in there (i.e. 12 hours for the duration of 1 day will run every 12 hours until you disable it)</li>
<li>Ensure the check for <strong>Enabled is checked</strong></li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-Trigger-Tab.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-Trigger-Tab.png" alt="Scheduled Task Trigger Tab - Automate Powershell Scripts" width="589" height="511" class="aligncenter size-full wp-image-2618" /></a><br />
&nbsp;</p>
<ul>
<li>Under the <strong>Actions tab</strong>, select <strong>start a program</strong> in the drop down menu</li>
<li>Under program/script select the <strong>powershell.exe</strong>. (i.e. c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)</li>
<li>Under <strong>Add arguments</strong>, select the location of the <strong>Powershell script</strong> (i.e. c:\_Scripts\Get-PasswordExpirationDate.ps1)</li>
</ul>
<p>&nbsp;</p>
<div id="blockquote1">
<strong>!Important</strong>: If you&#8217;re running a function, you&#8217;ll need to load it into memory if its not in an accessible module.  To do this, you&#8217;ll need to set the arguments to:<br />
<strong>&#8220;. C:\_Scripts\Get-PasswordExpirationDate.ps1; Get-PasswordExpirationDate -DaysWithinExpiration 10 -SendEmail&#8221;</strong>  &#8211;<em>Including quotes</em>
</div>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-Action-Tab.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-Action-Tab.png" alt="Scheduled Task Action Tab - Automate Powershell Scripts" width="1011" height="493" class="aligncenter size-full wp-image-2627" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-Action-Tab.png?v=1608969149 1011w, https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-Action-Tab-768x375.png?v=1608969149 768w" sizes="(max-width: 1011px) 100vw, 1011px" /></a></p>
<p>&nbsp;</p>
<ul>
<li>Under <strong>Conditions tab</strong>, leave default unless you want to change anything specifically</li>
<li>Under <strong>Settings tab</strong>, check <strong>Allow task to be run on demand</strong></li>
<li>Click OK and enter password for the account running the task</li>
</ul>
<h3>Granting Logon as batch job rights</h3>
<p>If the account you&#8217;re using to run the scheduled task is not an administrator, you might be prompted with the following message. &#8220;This task requires that the user account specified has Log on as batch job rights&#8230;<br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2020/12/Task-Scheduler-log-on-as-batch-rights.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/12/Task-Scheduler-log-on-as-batch-rights.png" alt="Task Scheduler log on as batch rights" width="951" height="354" class="aligncenter size-full wp-image-2629" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/12/Task-Scheduler-log-on-as-batch-rights.png?v=1608969752 951w, https://thesysadminchannel.com/wp-content/uploads/2020/12/Task-Scheduler-log-on-as-batch-rights-768x286.png?v=1608969752 768w" sizes="(max-width: 951px) 100vw, 951px" /></a></p>
<p>&nbsp;<br />
To fix that we&#8217;ll do the following:</p>
<ul>
<li>Go to <strong>start</strong> -> Type <strong>gpedit.msc</strong></li>
<li>Navigate to <strong>Computer Configuration</strong> -> <strong>Windows Settings</strong> -> <strong>Security Settings</strong> -> <strong>Local Policies</strong> -> <strong>User Rights Assignment</strong></li>
<li>Open <strong>Log on as a batch job</strong> and add the user account</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-Log-on-as-a-batch-job.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-Log-on-as-a-batch-job.png" alt="Scheduled Task Log on as a batch job" width="1018" height="585" class="aligncenter size-full wp-image-2632" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-Log-on-as-a-batch-job.png?v=1608970556 1018w, https://thesysadminchannel.com/wp-content/uploads/2020/12/Scheduled-Task-Log-on-as-a-batch-job-768x441.png?v=1608970556 768w" sizes="(max-width: 1018px) 100vw, 1018px" /></a></p>
<h2>When should you use Task Scheduler?</h2>
<p>Honestly there are a ton of use cases for using Task Scheduler and the possibilities are pretty much unlimited.  I would define it as anything that you can script and you want to run more than once in a specific time interval.  Off the top of my head, a perfect example would be <a href="https://thesysadminchannel.com/get-password-expiration-date-using-powershell-active-directory/" rel="noopener noreferrer" target="_blank">get password expiration dates using Powershell</a> and automatically notify the user.  Another good use case is automatically reboot a server after <a href="https://www.youtube.com/watch?v=a2AWwhUaQuw" rel="noopener noreferrer" target="_blank">deploying windows updates using SCCM</a>.</p>
<p>It really just depends on what you want to do.  There was a point in time where I had about 50+ jobs scheduled through out the week. Anything from Exchange Online to Active Directory tasks, if I was able to script it (and I usually can) I would setup a job to automate it.</p>
<h2>Automate Powershell Scripts &#8211; Conclusion</h2>
<p>At this point I really hope I was able to explain, in detail, how to automate your Powershell scripts with task scheduler so you can hopefully set it and forget it.  If you&#8217;re not using task scheduler, let me know in the comments what you are using to run your jobs.  Again, I strive to automate myself out of a job so it&#8217;s very helpful that I&#8217;m to run jobs essentially while I am sleeping.  This frees up time for me to try and automate more items or look for ways to enhance the environment on other technologies.</p>
<p>&nbsp;</p>
<p>If you liked this article, be sure to check out our other <a href="https://thesysadminchannel.com/server-administration/" rel="noopener noreferrer" target="_blank">Server Administration posts</a>.  I recently uploaded a <a href="https://thesysadminchannel.com/get-reboot-history-using-powershell/" rel="noopener noreferrer" target="_blank">Get Reboot History Powershell Script</a> that could be useful. While you&#8217;re at it, don&#8217;t forget to stop by our <a href="https://www.youtube.com/c/theSysadminChannel" rel="noopener noreferrer" target="_blank">YouTube Channel</a> for awesome video content.</p>
<p>The post <a href="https://thesysadminchannel.com/automate-powershell-scripts-with-task-scheduler/">Automate Powershell Scripts With Task Scheduler</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/automate-powershell-scripts-with-task-scheduler/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2458</post-id>	</item>
	</channel>
</rss>
