PeetersOnline.nl
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" |
14 Responses to Managing Scheduled Tasks Remotely Using Powershell
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





[...] Another take on scripting SCHTASKS.EXE Leave a Comment [...]
Awesome.
Invoke-Expression is unnecessary. You can simply state
schtasks.exe /query /s $ComputerName
This worked great. It worked with no issues. Thanks.
[...] Managing Scheduled Tasks Remotely Using Powershell | PeetersOnline – [...]
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
}
Great tip. Thanks!
very useful.
thanks very much.
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'}
Thank you.
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.
Hi, I would love to use the script but I’m rookie in PowerShell subject. How should I invoke the functions?
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.