Bootstrap FreeKB - Windows Task Scheduler - Run a PowerShell script on a schedule
Windows Task Scheduler - Run a PowerShell script on a schedule

Updated:   |  Windows Task Scheduler articles

To run a PowerShell script using the Task Scheduler, PowerShell's Execution Policy typically needs to be set to RemoteSigned. Use the Get-ExecutionPolicy cmdlet to view the current ExecutionPolicy. 

Get-ExecutionPolicy

 

If the Execution Policy is not set to RemoteSigned, use the the Set-ExecutionPolicy cmdlet to set the Execution Policy to RemoteSigned.

Set-ExecutionPolicy RemoteSigned

Lets create a PowerShell script that appends the current date and time to a file named DateTime.txt. 

  1. Select the Windows Start icon, in search type PowerShell, and select PowerShell ISE
  2. In the top panel of PowerShell ISE, enter the following code
$date = (Get-Date).ToString('MM/dd/yyyy hh:mm:ss')
$date >> C:\path\to\DateTime.txt
  1. Select File > Save as, and save the file as C:\Users\<username>\PowerShell_Scripts\current_date_time.ps1
  2. Press the green play button in PowerShell ISE
  3. Open the C:\path\to\DateTime.txt file to ensure the text file contains the current date/time.
  4. Press the green play button again, and ensure the DateTime.txt file contains a new line with the latest date/time.

To schedule the script using Task Scheduler:

  1. Select the Windows Start icon, type task in search, and select Task Scheduler
  2. On the right side of the Task Scheduler, select Create Task
  3. In the Create Task dialog box, make the following entries:
    1. General Tab
      • Give this task a name, such as PowerShell Currnet Date Time
      • At When running the task, use the following user account, ensure the user account being used has permission to write to all of the directories in your script.
      • Bullet Run whether user is logged on or not and check Do not store password
    2. Triggers Tab
      • Select New
      • Schedule the task to run today, just a few minutes in the future.
    3. Actions Tab
      • Select New
      • Keep the action as Start a program
      • In Program/script type C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
      • In the Add Argument box, type C:\Users\<username>\PowerShell_Scripts\current_date_time.ps1
      • Leave the Start In box empty
      • Select OK
    4. Conditions Tab
      • Remove the check from Start the task only if the computer is on AC power 
    5. Settings Tab
      • Check Run task as soon as possible after a scheduled start is missed
      • In Stop the task if it runs longer than, set the drop-down to 1 hour 
    6. Select OK
  4. Select OK to save the Task

 

Verify the task was successfully run:

In the left panel of Task Scheduler, select Task Scheduler Library, and the newly created PowerShell Current Date Time Task will be listed in the list of tasks. Before the task is run for the first time, Last Run Time will be never. After the task schedule has passed, right-click on Task Scheduler Library and select Refresh. The Last Run Time should display the time the task was run. Navigate to C:\Users\<username>\PowerShell_Scripts\ and open the current_date_time.txt file. Ensure the text file contains the date/time the task was scheduled to run.

 

Powershell and Task Scheduler nuiances

Some PowerShell scripts may work properly when in PowerShell, but fail to work properly when using Task Scheduler. Following are common issues:

  • Mapped network drive UNC - If your PowerShell script includes remote storage, use the UNC path to the remote storage in the PowerShell script. For example, use \\server\share instead of X:\. PowerShell will not use the network drives that are mapped in File Explorer.
  • Mapped network drive username / password - If your PowerShell script includes remote storage, and a username and password is required to map the network drive, the script may require logic to connect to the share with a username and password. When running the script in PowerShell, an error may appear, such as "Multiple connections to a server or shared resource by the same user." This message can be ignored.
$FS2 = "\\fs.example.com\share"
$username = "Your Username"
$password = "Your Password"

net use $FS2 $password /USER:$username  

 

 

Run silently

One issue with the scheduled task is that the PowerShell GUI appears every time the task is run. This is particularly problematic for scripts that are scheduled to run frequently, such as once every minute. To run the script silently, click here to download and install Powershell Community extensions (or go to http://pscx.codeplex.com/).. Then update the scheduled task, adding -WindowStyle Hidden to the Add Arguments field (example: C:\Users\<username>\PowerShell_Scripts\current_date_time.ps1 -WindowStyle Hidden).

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 822cbd in the box below so that we can be sure you are a human.