<img height="1" width="1" src="https://www.facebook.com/tr?id=1529264867168163&amp;ev=PageView &amp;noscript=1">

How to Migrate Scheduled Tasks from One Server to Another

With the end of life approaching for Windows Server 2003, a lot of admins are moving services from legacy 2003 servers to Windows Server 2012 R2.  A common task when performing this migration is to move scheduled tasks.  Windows provides some help with this by enabling you to export and import scheduled tasks. But there’s no migration tool to migrate all or only some of the scheduled tasks.  I pity the admin with 50 scheduled tasks running on an old Windows Server 2003 server who has to manually export and import every single one of those scheduled tasks. There has to be a better way. And it just so happens there is!  It’s called PowerShell.

By creating a PowerShell script, you can automate this migration. And you can make it as simple as running a single script—providing the source server name and the destination server name—and watching it work.  However, the script isn’t just going to write itself.  Unfortunately, you must do a little work up front. But I promise you’ll thank me after you’ve exported/imported your hundredth scheduled task.  In this article, I show you how to build your own script to easily migrate scheduled tasks from one server to another.

This script consists of three phases.

  1. Enumerating scheduled tasks on both the source server and destination server
  2. Prompting for any necessary passwords to create the scheduled tasks on the destination server
  3. Creating scheduled tasks on the destination server

Let’s walk through how to get this done. (But if you’d rather skip the walkthrough, feel free to download the script now.)

1.      Enumerating Scheduled Tasks on both the Source Server and Destination Server

The first step we should take is to enumerate all of the scheduled tasks on the source server.  Because our focus is on a 2003 to 2012 R2 migration, I’m going to assume you’re moving scheduled tasks from a Windows Server 2003 server.  Due to this limitation, we’re stuck using the Schedule.Service COM object rather than the built-in Get-ScheduledTask cmdlet. So we’ll have to create our own.

To do this, I’ve created a function called Get-MyScheduledTask.  This function will enable us to use the Schedule.Service COM object to find scheduled tasks on PowerShell v2 and higher machines.


The Get-MyScheduledTask function retrieves the root folder, all task subfolders, and all tasks from under those subfolders.  This function is called on the localhost to get the scheduled tasks from the source computer, but it uses PowerShell remoting to get the scheduled tasks from the destination computer.

Now that I've retrieved all of the scheduled tasks from the local computer, it's time to define the paths I’d like to exclude from the migration. To do this, I need to set up a PowerShell remoting session that I can reuse later as I create tasks on the destination computer.


2.      Prompting for Any Necessary Passwords to Create the Scheduled Tasks on the Destination Server

When scheduled tasks are created to run without a user logged on, you'll need to embed the password into the scheduled task.  Because I couldn’t readily see a method to export the passwords from the source computer’s tasks, I chose to implement another method (as you can see in the following screenshot).


This code snippet finds all of the user accounts that were configured on the source computer’s tasks.  It then prompts for the password to each of these accounts.  Once input, it then stores these accounts and passwords in a hash table to feed into the new tasks when they are created on the destination computer.

3.      Creating Scheduled Tasks on the Destination Computer

After we’ve gathered up all the scheduled tasks, we’re now ready to create them on the destination computer.  I wanted to make this script as flexible as possible. Let's go through a few lines.


You can see on line 101, I’m first checking to ensure the task doesn’t already exist on the destination computer.  This script also provides a $ExcludeTasks parameter.  I added this parameter here because I know sometimes you may not want to migrate all of the tasks.  By adding tasks to this array, you have the option not to migrate them.

On line 107, I’m giving you the option to only migrate scheduled tasks that are enabled by using the -SkipDisabledTasks parameter.

Now we finally get to creating the required tasks on the destination computer.  In the following screenshot, you can see I'm using a small snippet of script to look up the password to the user account for the particular tasks in the hash table we created earlier.  I’m then using that user account and password—along with the task’s XML, which contains the name, the path, and all of the settings—to recreate the task in the exact same way it was created on the source computer.


When this script is run and the -Verbose option is chosen, you will get output similar to the following screenshots—depending on the configuration of the source computer.




I hope this has given you insight into the process of migrating scheduled tasks and saves you some time.  Please let me know if you find any problems with the script. Or, if you see any areas for improvement, feel free to improve upon the script and share it with the community!  Some people need all the help they can get to make the move from Windows Server 2003!