Bootstrap FreeKB - PowerShell - Run a PowerShell script on a schedule using FBTaskScheduler
PowerShell - Run a PowerShell script on a schedule using FBTaskScheduler

Updated:   |  PowerShell articles

In order to be able to run a PowerShell script using the Task Scheduler, the Execution Policy typically needs to be set to RemoteSigned. To view the current Execution Policy, in PowerShell type Get-ExecutionPolicy. If the Execution Policy is not set to RemoteSigned, the Execution Policy can be set to RemoteSigned using the Set-ExecutionPolicy cmdlet.

Set-ExecutionPolicy RemoteSigned

 

Lets create a PowerShell script that outputs the current date and time to a file. 

  1. Select the Windows Start icon, in search type PowerShell, and select PowerShell ISE
  2. In the top panel of PowerShell ISE, enter the below code
  3. Select File > Save as, and save the file as C:\Users\<username>\PowerShell_Scripts\current_date_time.ps1
  4. Press the green play button in PowerShell ISE
  5. In Windows Explorer, navigate to C:\Users\<username>\PowerShell_Scripts\ and open the current_date_time.txt file. Ensure the text file contains the current date/time.
  6. Press the green play button again, and ensure the text file contains a new line with the latest date/time.
$date = Get-DateAdd-Content C:\Users\<username>\PowerShell_Scripts\current_date_time.txt " `n $date"

 

We next want to ensure we can run this script using PowerShell (not PowerShell ISE):

  1. Ensure PowerShell ISE is closed
  2. Select the Windows Start icon, in search type PowerShell, and select PowerShell
  3. Type C:\Users\<username>\PowerShell_Scripts\current_date_time.ps1 and press enter
  4. In Windows Explorer, navigate to C:\Users\<username>\PowerShell_Scripts\ and open the current_date_time.txt file. Ensure the text file contains the current date/time.

 

Follow these directions to automatically run this script using FBTaskScheduler.

  1. Download FBTaskScheduler from http://freebyte.com/fbtaskscheduler/
  2. Go to your Downloads folder and extract fbtaskscheduler.zip.
  3. In the fbtaskscheduler folder, run FBTaskScheduler.
  4. Click the + (plus) icon.
  5. In the Define new task dialog box:
    • Task name = Example
    • Program Location = Powershell.exe
    • Program Parameters = C:\Users\<username>\PowerShell_Scripts\current_date_time.ps1
    • 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. Nnavigate 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.




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 e2f053 in the box below so that we can be sure you are a human.