Creating a PowerShell Windows Scheduled Task

Recently setup some powershell tasks for managing my workstation. Documenting the steps.

By default powershell will not be able to execute scripts, one needs to launch the windows powershell command line tool in admin mode to correct this.

from the CLI ( as admin) run this command
set-executionpolicy remotesigned

Develop your powershell script

This example copies Google Chrome Bookmarks from Chrome folder to an archive location

$today = Get-Date -Format "MMddyyyy"
Copy-Item "C:\Users\dave\AppData\Local\Google\Chrome\User Data\Default\Bookmarks" -Destination "D:\archives\chrome-bookmarks-archive\Bookmarks$today"

Test it out from a command line ( this is how task scheduler will execute it)
powershell -F c:\scripts\archive-chrome-bookmarks.ps1

Open task scheduler from Computer management App

Create a new task from Task Scheduler app by clicking “Create Task”

Under the general tab select “Run whether user is logged in or not”

At the triggers tab click new and complete the scheduler task based on your needs

Under Actions tab
Ensure “Start a Program” is selected, Program/Script is “powershell” and in the arguments field add the arguments that you tested on the step #2. In the example case it was
“-F c:\scripts\archive-chrome-bookmarks.ps1”

When prompted input your usernames password to enable this to be run by the scheduler without interaction

Click the “Task Scheduler Library” link ( not the arrow) to see your tasks. You may have to right click and choose “refresh” in the section where the various tasks are located in order to see your new task. It did not automatically show up for me and this maybe a little confusing. In my opinion the Windows task scheduler user interface could use some updates to make it a little more intuitive. Right click on the task and choose “Run” to execute a practice run of your script.

If this fails for some reason you should review your original script to ensure it works on the command prompt. If it does and this run still fails you may need to dig into Windws event logs ( good luck with that).

339 Words