Operate Windows Task Scheduler via PowerShell: create scheduled tasks, manage schedules, view execution history. Suitable for operations that need to be executed automatically on a regular basis (such as backup, cleanup, sync).
Use cases
- The user says "help me back up files on a daily schedule" or "remind me to tidy up my desktop every Monday"
- Need to set up a startup task
- Need to execute a PowerShell script on a regular basis
- Need to view existing scheduled tasks
Command reference
View existing tasks
# 列出小搭子创建的任务
Get-ScheduledTask -TaskPath "\xiaodazi\" -ErrorAction SilentlyContinue |
Select-Object TaskName, State, @{N='NextRun';E={($_ | Get-ScheduledTaskInfo).NextRunTime}} |
Format-Table -AutoSize
# 列出所有用户任务
Get-ScheduledTask | Where-Object { $_.TaskPath -notlike "\Microsoft\*" } |
Select-Object TaskName, TaskPath, State |
Format-Table -AutoSize
# 查看任务详情
Get-ScheduledTask -TaskName "xiaodazi_backup" | Format-List *
Create scheduled task
# ===== 每天定时执行 =====
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -File `"C:\scripts\daily_backup.ps1`""
$trigger = New-ScheduledTaskTrigger -Daily -At "09:00"
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd
Register-ScheduledTask -TaskName "xiaodazi_daily_backup" -TaskPath "\xiaodazi\" -Action $action -Trigger $trigger -Settings $settings -Description "小搭子:每日文件备份"
# ===== 每周执行 =====
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At "08:00"
Register-ScheduledTask -TaskName "xiaodazi_weekly_cleanup" -TaskPath "\xiaodazi\" -Action $action -Trigger $trigger -Settings $settings -Description "小搭子:每周桌面整理"
# ===== 开机启动 =====
$trigger = New-ScheduledTaskTrigger -AtLogOn
Register-ScheduledTask -TaskName "xiaodazi_startup" -TaskPath "\xiaodazi\" -Action $action -Trigger $trigger -Settings $settings -Description "小搭子:开机启动任务"
# ===== 间隔执行(每 30 分钟) =====
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 30)
Register-ScheduledTask -TaskName "xiaodazi_sync" -TaskPath "\xiaodazi\" -Action $action -Trigger $trigger -Settings $settings -Description "小搭子:定时同步"
Manage tasks
# 手动执行
Start-ScheduledTask -TaskName "xiaodazi_daily_backup" -TaskPath "\xiaodazi\"
# 暂停任务
Disable-ScheduledTask -TaskName "xiaodazi_daily_backup" -TaskPath "\xiaodazi\"
# 恢复任务
Enable-ScheduledTask -TaskName "xiaodazi_daily_backup" -TaskPath "\xiaodazi\"
# 删除任务
Unregister-ScheduledTask -TaskName "xiaodazi_daily_backup" -TaskPath "\xiaodazi\" -Confirm:$false
View execution history
# 最近执行记录
Get-WinEvent -FilterHashtable @{LogName='Microsoft-Windows-TaskScheduler/Operational'; ID=102,201} -MaxEvents 20 |
Select-Object TimeCreated, @{N='TaskName';E={$_.Properties[0].Value}}, @{N='Result';E={$_.Properties[1].Value}} |
Format-Table -AutoSize
Naming conventions
- Task path is unified as
\xiaodazi\ - Task name uses the
xiaodazi_prefix - Description starts with "小搭子:"
Output conventions
- After creation, display the task name, trigger conditions, and next run time
- Display the list as a table, including name, status, and next run time
- Display the most recent 5 records of execution history
Security rules
- HITL confirmation is required before creating a task: display the complete execution command and trigger conditions
- HITL confirmation is required before deleting a task
- Do not create tasks with administrator privileges:
-RunLevel Limited - Only operate on
\xiaodazi\tasks under the path: do not modify system tasks or other application tasks - The script path to execute must be a file under the user directory