{"id":9992,"date":"2024-10-03T09:01:43","date_gmt":"2024-10-03T09:01:43","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/how-to-schedule-powershell-scripts-with-windows-task-scheduler\/"},"modified":"2025-01-20T10:20:57","modified_gmt":"2025-01-20T10:20:57","slug":"how-to-schedule-powershell-scripts-with-windows-task-scheduler","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/how-to-schedule-powershell-scripts-with-windows-task-scheduler\/","title":{"rendered":"How to Schedule PowerShell Scripts with Windows Task Scheduler"},"content":{"rendered":"<p><p>On Windows, the integrated Task Scheduler enables users to trigger actions based on schedules or specific events. This guide details the steps to set up a PowerShell script so that it runs automatically via the Windows Task Scheduler. The PS1 script will execute discreetly in the background, free from any pop-ups, and it operates independently of the current PowerShell script execution policy settings.<\/p>\n<p>For this instance, I will configure the <code>C:PSOutlook_Email_to.ps1<\/code> PowerShell script to run every 10 minutes.<\/p>\n<ol>\n<li>Launch the <strong>Task Scheduler<\/strong> console using the command <code>taskschd.msc<\/code>.<\/li>\n<li>Expand the Task Scheduler library tree and create a dedicated folder for your personal scheduled tasks for easier management. Right-click and choose <strong>Create Task<\/strong>.<\/li>\n<li>Under the <strong>General<\/strong> tab, enter a name for the task and specify the user context in which it will operate. The task can be set to run:<\/li>\n<ul>\n<li><code>Run only when the user is logged in<\/code><\/li>\n<li><code>Run whether the user is logged on or not<\/code><\/li>\n<\/ul>\n<p>The latter option is commonly used. If you choose this, you may need to provide the credentials of a specific user (passwords can be securely stored using the <a href=\"https:\/\/woshub.com\/saved-passwords-windows-credential-manager\" rel=\"nofollow noopener\" target=\"_blank\">Credentials Manager<\/a>). If elevated permissions are necessary, select the <strong>Run with highest privileges<\/strong> option.<\/p>\n<p>In environments with Active Directory, tasks can also be scheduled to run using <a href=\"https:\/\/woshub.com\/group-managed-service-accounts-in-windows-server-2012\/\" rel=\"nofollow noopener\" target=\"_blank\">gMSA managed service accounts<\/a>.<\/p>\n<\/li>\n<li>In the <strong>Triggers<\/strong> tab, set the event or timing for when the task should start. For instance, to initiate a task when a user logs in, select the <strong>At log on<\/strong> trigger and configure it to run every <strong>10<\/strong> minutes under the <strong>Repeat task every<\/strong> option.<\/li>\n<li>For tasks running as SYSTEM or a user with stored passwords, opt to execute the task upon system startup (<strong>At startup<\/strong>) and establish a regular restart schedule.<\/li>\n<li>You can also select the <strong>On a schedule<\/strong> trigger to specify precise start times. It&#8217;s possible to create multiple triggers for one task. Additionally, the scheduler can run a task triggered by specific events logged in the Event Viewer (refer to <a href=\"https:\/\/woshub.com\/schedule-task-to-start-when-another-task-finishes\/\" rel=\"nofollow noopener\" target=\"_blank\">this guide<\/a> for more details).<\/li>\n<li>Next, navigate to the <strong>Actions<\/strong> tab. Here, define the action that will execute upon the trigger event. In this scenario, I will select to run a PowerShell script. Choose <strong>New<\/strong> -&gt; <strong>Start a program<\/strong>. Input the action details as follows:<\/li>\n<ul>\n<li>Program\/script: <code>powershell.exe<\/code><\/li>\n<li>Add arguments (optional):<code> -ExecutionPolicy Bypass -NonInteractive -WindowStyle Hidden -File \"C:PSOutlook_Email_to.ps1\"<\/code><\/li>\n<\/ul>\n<p>Before launching the script via Task Scheduler, verify that it does not return any errors when run in unattended mode. Use the command:<\/p>\n<p><code>powershell.exe -file C:PSOutlook_Email_to.ps1 -NoExit<\/code><\/p>\n<\/li>\n<li>The following parameters are employed to run a PowerShell script:<br \/><code>-File<\/code>: Specifies the complete path to the script file (PS1).<br \/><code>-ExecutionPolicy<\/code>: Defines the execution policy for the current PowerShell session, ignoring current settings if <strong>Bypass<\/strong> is specified.<br \/><code>-NonInteractive<\/code>: Prevents any interactive prompts from appearing.<br \/><code>-WindowStyle Hidden<\/code>: Conceals the PowerShell window during execution (the console may briefly appear). This is applicable for scripts initiated from session 0 only.<br \/><code>-NoProfile<\/code>: Use this if the script can execute without accessing a user profile, which can speed up execution.<\/li>\n<li>In the <strong>Settings<\/strong> tab, consider enabling options such as:<br \/><strong>Allow task to be run on demand<\/strong><br \/><strong>If the running task does not end when requested, force it to stop<\/strong><br \/><strong>Do not start a new instance<\/strong><\/li>\n<li>Finally, save the task settings and ensure it appears in the <strong>Task Scheduler<\/strong> interface. Click on the task and select <strong>Run<\/strong> to test its functionality.<\/li>\n<\/ol>\n<p>If the PowerShell script executes successfully, the Last Run Result will show a confirmation message such as:<\/p>\n<pre>The operation completed successfully (0x0).<\/pre>\n<div class=\"info_box\">To log all activity in a text file, consider adding a simple <a href=\"https:\/\/woshub.com\/write-output-log-files-powershell\/\" rel=\"nofollow noopener\" target=\"_blank\">logging function to the PowerShell script<\/a>. This will enable you to review detailed records of all actions performed at any time.<\/div>\n<li>Utilize the <strong>History<\/strong> tab to check the history and outcomes of previous task runs. By default, Task History isn&#8217;t saved (click the <strong>Enable All Tasks History<\/strong> link in the Actions pane).<\/li>\n<\/ol>\n<p>You can also <a href=\"https:\/\/woshub.com\/how-to-create-scheduled-task-using-powershell\/\" rel=\"nofollow noopener\" target=\"_blank\">create a scheduled task<\/a> to execute a PowerShell script directly from the command line.<\/p>\n<\/p>\n<p><p><code>$TaskName=\"CheckOutlookMailbox\"<br \/> $Trigger = New-ScheduledTaskTrigger -AtStartup<br \/> $Trigger.Repetition = (New-ScheduledTaskTrigger -once -at \"12am\" -RepetitionInterval (New-TimeSpan -Minutes 10) -RepetitionDuration (New-TimeSpan -Minutes 10)).repetition<br \/> $User= \"NT AUTHORITYSYSTEM\"<br \/> $Action= New-ScheduledTaskAction -Execute \"PowerShell.exe\" -Argument \"-ExecutionPolicy Bypass -NonInteractive -WindowStyle Hidden -File C:PSOutlook_Email_to.ps1\"<br \/> Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force<\/code><\/p>\n<\/p>\n<p><p>When executing PowerShell scripts via the Windows Task Scheduler, there are several important considerations:<\/p>\n<\/p>\n<ul>\n<li>To execute the script in the <a href=\"https:\/\/woshub.com\/install-update-powershell-windows\/\" rel=\"nofollow noopener\" target=\"_blank\">PowerShell Core<\/a> environment, use <code>pwsh.exe<\/code> rather than <code>powershell.exe<\/code>.<\/li>\n<li>If other users have access to the machine where you are running the PowerShell script with elevated permissions, ensure you have modified the NTFS access permissions on the PS1 file to prevent them from altering it.<\/li>\n<li>If the task is executed as a user without administrative privileges, their account needs to be included in the local security policy<strong> Log on as a batch job (<\/strong><a href=\"https:\/\/woshub.com\/group-policy-editor-gpedit-msc-for-windows-10-home\/\" rel=\"nofollow noopener\" target=\"_blank\">gpedit.msc<\/a> <strong>-&gt; Computer Configuration<\/strong> -&gt; <strong>Windows Settings<\/strong> -&gt; <strong>Security Settings<\/strong> -&gt; <strong>Local Policies<\/strong> -&gt; <strong>User Rights Assignment).<\/strong> A warning message will appear when setting up such a task:\n<pre>This task requires that the user account specified has Log on as batch job rights<\/pre>\n<\/li>\n<li>In an Active Directory environment, the <a href=\"https:\/\/woshub.com\/running-powershell-startup-scripts-using-gpo\/\" rel=\"nofollow noopener\" target=\"_blank\">Group Policy Object (GPO) can be utilized to run PowerShell scripts<\/a> during user logon or logoff, or when a computer starts up or shuts down. These scripts are referred to as <strong>logon scripts<\/strong>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>On Windows, the integrated Task Scheduler enables users to trigger actions based on schedules or specific events. This guide details the steps to set up a PowerShell script so that it runs automatically via the Windows Task Scheduler. The PS1 script will execute discreetly in the background, free from any pop-ups, and it operates independently [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9993,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117,99,108],"tags":[],"class_list":["post-9992","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-windows-11","category-windows-server-2022"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=9992"}],"version-history":[{"count":2,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9992\/revisions"}],"predecessor-version":[{"id":10368,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9992\/revisions\/10368"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/9993"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=9992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=9992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=9992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}