Managing Scheduled Tasks Remotely Using Powershell

The following Powershell functions allow you to manage querying, creating and removing scheduled tasks on one or more computers remotely.
The functions use schtasks.exe, which is included in Windows. Unlike the Win32_ScheduledJob WMI class, the schtasks.exe commandline tool will show manually created tasks, as well as script-created ones. The examples show some, but not all parameters in action. I think the parameter names are descriptive enough to figure it out, really. If not, take a look at schtasks.exe /?. One tip: try piping a list of computer names to foreach-object and into this function.


Enjoy.
Hugo

Function Get-ScheduledTask
	{
	param([string]$ComputerName = "localhost")
	Write-Host "Computer: $ComputerName"
	$Command = "schtasks.exe /query /s $ComputerName"
	Invoke-Expression $Command
	Clear-Variable Command -ErrorAction SilentlyContinue
	Write-Host "`n"
	}
 
# EXAMPLE: Get-ScheduledTask -ComputerName Server01
 
Function Remove-ScheduledTask
	{
	param(
	[string]$ComputerName = "localhost",
	[string]$TaskName = "blank"
	)
	If ((Get-ScheduledTask -ComputerName $ComputerName) -match $TaskName)
		{
		If ((Read-Host "Are you sure you want to remove task $TaskName from $ComputerName(y/n)") -eq "y")
			{
			$Command = "schtasks.exe /delete /s $ComputerName /tn $TaskName /F"
			Invoke-Expression $Command
			Clear-Variable Command -ErrorAction SilentlyContinue
			}
		}
	Else
		{
		Write-Warning "Task $TaskName not found on $ComputerName"
		}
	}
 
# EXAMPLE: Remove-ScheduledTask -ComputerName Server01 -TaskName MyTask
 
Function Create-ScheduledTask
	{
	param(
	[string]$ComputerName = "localhost",
	[string]$RunAsUser = "System",
	[string]$TaskName = "MyTask",
	[string]$TaskRun = '"C:\Program Files\Scripts\Script.vbs"',
	[string]$Schedule = "Monthly",
	[string]$Modifier = "second",
	[string]$Days = "SUN",
	[string]$Months = '"MAR,JUN,SEP,DEC"',
	[string]$StartTime = "13:00",
	[string]$EndTime = "17:00",
	[string]$Interval = "60"	
	)
	Write-Host "Computer: $ComputerName"
	$Command = "schtasks.exe /create /s $ComputerName /ru $RunAsUser /tn $TaskName /tr $TaskRun /sc $Schedule /mo $Modifier /d $Days /m $Months /st $StartTime /et $EndTime /ri $Interval /F"
	Invoke-Expression $Command
	Clear-Variable Command -ErrorAction SilentlyContinue
	Write-Host "`n"
	}
 
# EXAMPLE: Create-ScheduledTask -ComputerName MyServer -TaskName MyTask02 -TaskRun "D:\scripts\script2.vbs"

»crosslinked«

14 thoughts on “Managing Scheduled Tasks Remotely Using Powershell”

  1. Pingback: Getting Scheduled Tasks in Powershell « PowerShell Station
  2. This was very helpfull. I added a filter, that converts the returned data to objects:
    filter ConvertFrom-CmdList {

    $_ | foreach {if ($_ -match ‘^$’){ $newobj = New-Object Object
    $obj | foreach {$newobj | Add-Member NoteProperty $($_ -split ‘:’)[0] “$($_ -replace ‘^.*:[ ]+’)”}
    $newobj
    $obj = @()}
    if ($_ -notmatch ‘^$’){$obj += $_ }
    }
    }

    Function Get-ScheduledTask {

    param([string]$ComputerName = “localhost”)
    Write-Host “Computer: $ComputerName”
    schtasks.exe /query /s $ComputerName /FO List | ConvertFrom-CmdList
    }

  3. If you want prettier output from schtasks /query, you can easily convert it to an object:

    $Tasks = schtasks.exe /QUERY /V /FO:CSV /S:$ComputerName | ConvertFrom-Csv

    /V Gives you much more info
    /FO:CSV formats the output into csv so you can convert it right into an array of task objects. I tend to throw in a where statement too to get rid of the artifacts and built in tasks.

    $Tasks = schtasks.exe /QUERY /V /FO:CSV /S:$ComputerName | ConvertFrom-Csv | Where-object {$_.taskName -notlike '\Windows*' -and $_.taskname -notlike 'taskname'}

  4. Hi,
    I would like to know who invoked the scheduled task.

    I user one PS script to invoke an already configured scheduled task that is set to run with high privileges. As part of its execution, the scheduled tasks writes output to htmm file. Is there anyway we can get who has invoked the scheduled task using the firs PS script. I would like to add their name to the output of the scheduled task script.

  5. Hi, I am migrating schedule task from windows server 2003 to windows server 2008. One way is from Task scheduler GUI using export from and import to technique and then edit the arguments in Action. I am interested in knowing how to do it using a powershell script for automation using PS version 2.0. The version 3.0 has the cmdlets but i am not thinking of using it.

Leave a Reply