Running Powershell scripts with Task Scheduler

Scheduling a Powershell script with Task Scheduler

I do a fair amount of script writing to make my life easier. If it’s a repeated job, I try and automate it so that my time is freed up for more important (interesting) things. This is especially true if the task is tedious (with many manual steps) or involves lots of waiting around (like an Exchange CU). I have a fair number of Powershell scripts which run from Task Scheduler under Windows Server 2016. There are a lot of options to configure, so this can be complex. I’ll share the cheatcode way of doing this at the end.

  1. Load Task Scheduler.
  2. Click Create Task
  3. Under the General tab, fill in a Name and Description. We’ll circle back to the other options at the end.
  4. Click Triggers. Most of my jobs run on a time-based schedule, not events (like computer startup). Setup the schedule that you need.
  5. Under the Actions tab, click New. Another window will appear. Leave the Action as Start a program.
    If the script runs under Powershell 5.1, add Powershell to the Program/Script field.
    If the script runs under Powershell 7, add "C:\Program Files\PowerShell\7\pwsh.exe" to the Program/Script field. It’s important to have the quotes so that the parser doesn’t freak out over the space in ‘Program Files’.
    Add the following Arguments: -executionpolicy bypass -command "& '\\SERVER\SCRIPT REPOSITORY \script.ps1'". Again, I need double quotes around my UNC because of the space.
  6. I usually clear the Power options under the Conditions tab. If the OS thinks that our server is now running on battery power, we have bigger issues.
  7. Under Settings, I usually adjust Stop the task if it runs longer than… field to something appropriate for the job schedule.
  8. Back to the General tab. Under the Security Options, I add the service account that my scripts run under. I set the task to Run whether the user is logged in or not, with highest priviledges to bypass any UAC problems and Configure for Windows Server 2016. (I have no idea what the real-world implications of setting the OS field are, since my scripts seem to work with this is also set to ‘Windows Server 2008’).
  9. Click OK. You’ll be prompted for the password of the credentials you used in #8. At the end of all this, I like to manually run the task by right-clicking it. This is a final sanity check that everything works. It’s easier for me to check at the time of setup (instead of the first time it runs) because my brain is already in that space.

Since I promised the cheatcode way of doing this, here’s how I like to add new tasks that are similar to existing ones. Since most of mine are scripts, the Task Scheduler settings are pretty much identical. This method helps avoid problems when editing the Arguments field - the string is so long and the field window is so small that I always have to scroll and I hate trying to figure out if I’ve keyed things in properly.

  1. In Task Scheduler, Find a task that closely matches the new one you’re implementing.
  2. Select it, right-click and choose Export.
  3. Save the XML file someone easily accessed, like the Desktop.
  4. Right-click the resulting file and open it in Notepad (or the editor of your choice).
  5. Update the <Description>, <URI> and <Arguments> fields with the correct values.
  6. Save the file.
  7. Back in Task Scheduler, select Import Task.
  8. Browse to the XML file you just edited, and Open it.
  9. Task scheduler will add a new task. You’ll have to re-enter the password and possibly adjust the schedule, but everything else should be correct.
Built with Hugo
Theme Stack designed by Jimmy