1

How can I modify the action step in a scheduled task? We have hundreds of scheduled tasks that point to a particular path and run a PowerShell script. How can we find those tasks and then change the path in the action step without deleting and recreating the entire task?

  • Export the tasks as .xml, run a find+replace, then import the tasks. – Wutnaut Jun 4 '14 at 18:08
  • I saw that you have HUNDREDS of scheduled tasks and found out the scheduled tasks module will only let you export 1 task at a time, so you may find this resource handy, especially since you're already working in PS: petri.co.il/export-scheduled-tasks-using-powershell.htm – Wutnaut Jun 4 '14 at 18:18
1

Scheduled tasks are contained in C:\Windows\System32\Tasks\ and contain XML files. While the Petri article is a good solution for Windows 8 and Windows Server 2012, that's not a complete solution. This should allow you to find the tasks with a specific command or argument, and replace those.

$computer = "localhost"

$oldCommand = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$oldArguments = "-File `"C:\Users\Public\Scripts\oldScript.ps1`""
$newCommand = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$newArguments = "-File `"C:\Users\Public\Scripts\newScript.ps1`""

$tasks = Get-ChildItem "\\$computer\c$\Windows\System32\Tasks\" | Where-Object {
    $_.PSIsContainer -eq $false `
    -and `
    (([xml](Get-Content -Path $_.FullName)).Task.Actions.Exec.Command -like $oldCommand) `
    -and `
    (([xml](Get-Content -Path $_.FullName)).Task.Actions.Exec.Arguments -like $oldArguments)
    }

$tasks | ForEach-Object {
    $xml = [xml](Get-Content -Path $_.FullName)
    $xml.Task.Actions.Exec.Command = $newCommand
    $xml.Task.Actions.Exec.Arguments = $newArguments
    $xml.Save($_.FullName)
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.