skip to main content
ANIMESH BULUSU | అనిమేష్ బులుసు

Create scheduled tasks on Windows Server boxes

I wrote a script today to create scheduled tasks on different Windows Server boxes via PowerShell.

$conf = (resolve-path "config")
$saturdayServers = (gc $conf\dbs1.txt)
$sundayServers = (gc $conf\dbs2.txt)

foreach($server in $saturdayServers)
  write-host $server
  SCHTASKS /Create /SC weekly /D SAT /TN cleanup-old-backups /ST 06:00:00 /TR E:\Project\maintenance\cleanup.bat /S $server /RU DOMAIN\username /RP passw0rd 

foreach($server in $sundayServers)
  write-host $server
  SCHTASKS /Create /SC weekly /D SUN /TN cleanup-old-backups /ST 00:00:00 /TR E:\Project\maintenance\cleanup.bat /S $server /RU DOMAIN\username /RP passw0rd

SCHTASKS is a command line interface to Windows scheduled tasks program.

The first loop creates a weekly scheduled task called cleanup-old-backups and sets it to run on Saturday at 6AM on each server.

The second loop does the same thing except it runs on 12AM on Sundays.