如何使用 PowerShell 导出计划任务
我们喜欢将所有计划任务迁移到另一台服务器或计算机上。与其在任务计划程序中单独导出计划任务,不如运行 PowerShell 脚本为您批量导出所有任务。运行脚本并保存计划任务以进行备份也非常好。在本文中,您将学习如何使用 PowerShell 导出计划任务。
在任务计划程序中导出计划任务
无法从任务计划程序程序导出多个或所有计划任务。只能导出单个任务计划。
选择任务计划程序中的所有任务,没有导出的选项。
在任务计划程序中选择单个任务,您可以导出该任务。
如果您有很多计划任务,这可能会非常耗时。那么,更好的方法是什么? PowerShell 来救援。
要导出所有计划任务,我们将使用获取计划任务用于检索它们的 PowerShell cmdlet 和导出计划任务用于导出它们的 PowerShell cmdlet。
让我们在下一步中研究一下。
下载 Export-SchedTasks PowerShell 脚本
上创建两个文件夹(中:)驾驶:
参见:防止在 Windows 11 中创建新计划任务的行之有效的方法
- 脚本
- 任务
下载 Export-SchedTasks.ps1 PowerShell 脚本并将其放置在C:脚本文件夹。该脚本会将所有计划任务导出到C:任务文件夹。
笔记:它不会导出包含计划任务的 Microsoft 文件夹,因为它们是默认任务。
确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。
另一种选择是将以下代码复制并粘贴到记事本中。给它起个名字导出-SchedTasks.ps1并将其放置在C:脚本文件夹。
<#
.SYNOPSIS
Export-SchedTasks.ps1
.DESCRIPTION
Export Windows Scheduled Tasks on Windows Server and Windows Clients for backup purposes.
.LINK
www.alitajran.com/export-scheduled-tasks-powershell/
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 12/08/2023 - Initial version
#>
# Define the backup path
$backupPath = "C:Tasks"
# Get the unique task folders from the scheduled tasks
$taskFolders = (Get-ScheduledTask).TaskPath | Where-Object { ($_ -notmatch "Microsoft") } | Select-Object -Unique
# Start exporting of scheduled tasks
Write-Host "Start exporting of scheduled tasks." -ForegroundColor Cyan
# Check if the backup path exists
if (Test-Path -Path $backupPath) {
Write-Host "Folder already exists: $backupPath" -ForegroundColor Yellow
}
else {
# Create the backup path if it doesn't exist
New-Item -ItemType Directory -Path $backupPath | Out-Null
Write-Host "Backup path created: $backupPath" -ForegroundColor Green
}
# Loop through each task folder
foreach ($taskFolder in $taskFolders) {
Write-Host "Task folder: $taskFolder" -ForegroundColor Cyan
# Check if the task folder is not the root folder
if ($taskFolder -ne "") {
$folderPath = "$backupPath$taskFolder"
# Create the task folder in the backup path if it doesn't exist
if (-not (Test-Path -Path $folderPath)) {
New-Item -ItemType Directory -Path $folderPath | Out-Null
}
else {
Write-Host "Folder already exists: $folderPath" -ForegroundColor Yellow
}
}
# Get the tasks in the task folder
$tasks = Get-ScheduledTask -TaskPath $taskFolder -ErrorAction SilentlyContinue
# Loop through each task in the task folder
foreach ($task in $tasks) {
$taskName = $task.TaskName
# Export the task and save it to a file
$taskInfo = Export-ScheduledTask -TaskName $taskName -TaskPath $taskFolder
$taskInfo | Out-File "$backupPath$taskFolder$taskName.xml"
Write-Host "Saved file $backupPath$taskFolder$taskName.xml" -ForegroundColor Cyan
}
}
# Exporting of scheduled tasks finished
Write-Host "Exporting of scheduled tasks finished." -ForegroundColor Green
这就是它的样子。
运行导出计划任务 PowerShell 脚本
运行 Export-SchedTasks.ps1 PowerShell 脚本以获取所有任务计划任务并将它们导出到C:任务文件夹。
C:scripts.Export-SchedTasks.ps1
输出显示:
Start exporting of scheduled tasks.
Backup path created: C:Tasks
Task folder:
Saved file C:TasksCreateExplorerShellUnelevatedTask.xml
Saved file C:TasksMicrosoftEdgeUpdateTaskMachineCore{68B94FCC-61AA-45EA-B214-C666C5A7C344}.xml
Saved file C:TasksMicrosoftEdgeUpdateTaskMachineUA{B5BEDAA1-3440-4D7D-A459-0A8C98600F11}.xml
Saved file C:TasksUser_Feed_Synchronization-{D86918D5-2FFC-4B1D-9AF1-0B66C68F64B2}.xml
Saved file C:Taskswin-acme renew (acme-v02.api.letsencrypt.org).xml
Task folder: Mozilla
Saved file C:TasksMozillaFirefox Background Update 308046B0AF4A39CB.xml
Saved file C:TasksMozillaFirefox Default Browser Agent 308046B0AF4A39CB.xml
Exporting of scheduled tasks finished.
验证计划任务 XML 文件
Export-SchedTasks.ps1 PowerShell 脚本将所有任务批量导出到 XML 文件。找到路径中的XML文件C:任务。
使用您喜欢的应用程序打开 XML 文件。例如,Microsoft Edge、Notepad 或 Notepad++。
XML 文件看起来很棒。
这是否有助于您使用 PowerShell 将计划任务备份到 XML 文件?
结论
您学习了如何使用 PowerShell 导出计划任务。首先,运行 Export-SchedTasks PowerShell 脚本。接下来,进入导出文件夹,查看所有导出的XML文件中的计划任务。该脚本适用于 Windows Server 和 Windows 客户端。
您喜欢这篇文章吗?您可能还喜欢 Windows Server 安装后配置。不要忘记关注我们并分享这篇文章。
