Manage Sheduled Tasks with PowerShell

Manage Sheduled Tasks with PowerShell

Hi All,

I’ve had some work to do with Scheduled Tasks and schedule a PowerShell Script with PowerShell. This Article shows you how you can list, create, export and import Scheduled Tasks.

List PowerShell Commands for Scheduled Tasks

Get-Command *ScheduledTask*

These are the Scheduled Tasks on the Server

List Scheduled Tasks Get-ScheduledTask

Get-ScheduledTask

List specific ScheduledTask

$ScheduledTask = Get-ScheduledTask -TaskName ZIPandDeleteIISLog
$ScheduledTask | fl
$ScheduledTask.Actions
$ScheduledTask.Triggers

Details Get-ScheduledTaskInfo

Get-ScheduledTask -TaskName ZIPandDeleteIISLog | Get-ScheduledTaskInfo

Create new Scheduled Task Register-ScheduledTask

#Run only when user is logged on
$Trigger = New-ScheduledTaskTrigger -Daily -At 1am
$Action = New-ScheduledTaskAction -Execute 'PowerShell' -Argument '-NoProfile C:\Scripts\ZIPandDeleteIISLog.ps1'
$Settings = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -AllowStartIfOnBatteries -ExecutionTimeLimit (New-TimeSpan -Days 1)
Register-ScheduledTask -TaskName 'Demo PowerShell' -Action $Action -Trigger $Trigger -Settings $Settings

Result

Create new Sheduled Task with “Run whether user is logged on or not” and “Run with highest privileges”

#Run whether user is logged on or not / Run with highest privileges
$Trigger = New-ScheduledTaskTrigger -Daily -At 1am
$Action = New-ScheduledTaskAction -Execute 'PowerShell' -Argument '-NoProfile C:\Scripts\ZIPandDeleteIISLog.ps1'
$Settings = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -AllowStartIfOnBatteries -ExecutionTimeLimit (New-TimeSpan -Days 1)
Register-ScheduledTask -TaskName "Demo PowerShell High" -Action $Action -Trigger $Trigger -Settings $Settings -User "CORP\Administrator" -Password 'YourSecretPassword' -RunLevel Highest

Result

Export Sheduled Task to XML File

Export-ScheduledTask -TaskName "Demo PowerShell"
Export-ScheduledTask -TaskName "Demo PowerShell" | Set-Content -Path C:\Temp\Task.xml

Import Sheduled Task from XML File

[String]$XML = Get-Content -Path "C:\Temp\Task.xml"
Register-ScheduledTask -TaskName "ImportTask" -XML $XML -User "corp\Administrator" -Password "YourSecretPassword"

Result

Regards
Andres Bohren

PowerShell Logo

Windows Logo