Run PowerShell Script from Task Scheduler

Earlier we were talking on how to run PowerShell script on remote computer. Now let’s take a look at the basics of creating scheduled job in Windows Task Scheduler to run PowerShell script. For example, we have TestPSScript.ps1 script that is needed to run on a daily basis. There are two ways to solve this situation.

Create Scheduler Task from GUI

Open Task Scheduler snap-in (it can be found in the Administrative Tools, or by pressing Win + R and typing taskschd.msc) and select Create Task from right pane.

task scheduler powershell

Now specify the name and description of the task on the General tab. If necessary, specify the user on whose behalf the task will be run. For the task to be performed regardless of whether the user is logged in the system, select the option «Run whether user is logged on or not». When a task requires elevated privileges, then mark the option «Run with highest privileges».

powershell task scheduler

Now go to the Triggers tab and create a new trigger (New), which runs the job in our schedule that will be stored. Specify the start date and time in the Start field and in Expire field the date and time the job is finished. Specify to perform a task Daily and set the repeat time (Recur every) 1 Day.

READ ALSO  PowerShell: Switch Statement Usage

run powershell script from task scheduler

Then go to Action tab and specify an action for a scheduled task. Let us remind you that for security purposes PowerShell script can be executed only interactively, so at first you need to run PowerShell process and specify the path to the script in it. Therefore, in the Action specify to start powershell.exe, and in the field Add Arguments -File option and the path to our script, like this:

-File ″C:\PS\TestPSScript.ps1″

You can specify additional arguments:

  • -Command – Performs the command and any passing parameters, for example:
    -Command ″& {C:\PS\TestPSScript.ps1 –param1 1 –param2 22}″
  • -NonInteractive — disable this interactive console to the user;
  • -WindowStyle Hidden — run PowerShell window in hidden mode, invisibly to the user;
  • -NoProfile — prevents the user profile loading, which can slightly speed up the execution of script;
  • -NoExit — leave the shell open after execution of the script. This can be useful when testing and debugging;
  • -ExecutionPolicy — sets the script execution policy for the current session, it can be set to Unrestricted, RemoteSigned, AllSigned or Restricted. The specified policy will be effective only in the current session and takes precedence over any previously established policies.

Note. Usually it is enough to set value RemoteSigned for normal scripts. This policy allows you to run scripts prepared on the local computer without limitation. Scripts downloaded from the Internet are started only if they are signed by the digital signature. You can change the current policy with the help of Set-ExecutionPolicy, for example:

Set-ExecutionPolicy RemoteSigned –force

task scheduler powershell script

Fill in the necessary fields, click OK and save the task. Now, the script will start on a schedule on daily basis at the specified time.

READ ALSO  Change Default OU permissions in Active Directory

Create  Schedule Job using PowerShell 3.0 and higher

In PowerShell 3.0 on Windows 8 and Windows Server 2012 and higher, a new functional Scheduled Job appears. It gives the ability to create scheduled tasks from the PowerShell command console without using the Task Scheduler snap-in.

At first, create a schedule for running:

$t_trigger = New-JobTrigger -Daily -At 12:00PM

Then save credential to the variable:

$cred_saved = Get-Credential corpobama

Specify the job to run with elevated privileges:

$elevated_job = New-ScheduledJobOption -RunElevated

And register the task with name Start:

Register-ScheduledJob -Name Start -FilePath C:\PS\TestPSScript.ps1 -Trigger $t_trigger -Credential $cred_saved -ScheduledJobOption $elevated_job

task scheduler run powershell script

To make sure that the job is created, you need to open the Task Sheduler and find the task under Microsoft\Windows\PowerShell\SheduledJobs that was created earlier.

powershell script task scheduler

Tip. For each scheduled task, in the directory %systemdrive%\Users\%username%\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs PowerShell creates a folder with the same name. This folder contain XML file with task settings and folder Output, that stores the job history in the files Result.xml and Status.xml. These files can be useful for debugging and diagnostics if the task that doesn’t work properly.

Cyril Kardashevsky

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.