Sometimes you want to get a count of mailbox/mailboxes per database in Exchange. But, why…
How to export scheduled tasks with PowerShell
We like to migrate all the scheduled tasks to another server or computer. Instead of exporting the scheduled tasks individually in Task Scheduler, running a PowerShell script is better to bulk export all tasks for you. It’s also excellent to run the script and save the scheduled tasks for backup purposes. In this article, you will learn how to export scheduled tasks with PowerShell.
Table of contents
Export scheduled task in Task Scheduler
There is no way to export multiple or all scheduled tasks from the Task Scheduler program. It’s only possible to export a single Task schedule.
Select all the tasks in the Task Scheduler, and there is no option to export.
Select a single task in Task Scheduler, and you can export that task.
If you have many scheduled tasks, this can be highly time-consuming. So, what is a better approach? PowerShell to the rescue.
Export all scheduled tasks with PowerShell
To export all the scheduled tasks, we will use the Get-ScheduledTask PowerShell cmdlet to retrieve them and the Export-ScheduledTask PowerShell cmdlet to export them.
Let’s look into that in the next step.
Download Export-SchedTasks PowerShell script
Create two folders on the (C:) drive:
- Scripts
- Tasks
Download and place Export-SchedTasks.ps1 PowerShell script in the C:\Scripts folder. The script will export all the scheduled tasks to the C:\Tasks folder.
Note: It will not export the Microsoft folder that contains scheduled tasks because they are default tasks.
Ensure the file is unblocked to prevent errors when running the script. Read more in the article Not digitally signed error when running PowerShell script.
Another option is to copy and paste the below code into Notepad. Give it the name Export-SchedTasks.ps1 and place it in the C:\Scripts folder.
<#
.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
This is how it looks.
Run Export Scheduled Tasks PowerShell script
Run the Export-SchedTasks.ps1 PowerShell script to get all the Task Scheduled tasks and export them to XML files in the C:\Tasks folder.
C:\scripts\.\Export-SchedTasks.ps1
The output shows:
Start exporting of scheduled tasks.
Backup path created: C:\Tasks
Task folder: \
Saved file C:\Tasks\CreateExplorerShellUnelevatedTask.xml
Saved file C:\Tasks\MicrosoftEdgeUpdateTaskMachineCore{68B94FCC-61AA-45EA-B214-C666C5A7C344}.xml
Saved file C:\Tasks\MicrosoftEdgeUpdateTaskMachineUA{B5BEDAA1-3440-4D7D-A459-0A8C98600F11}.xml
Saved file C:\Tasks\User_Feed_Synchronization-{D86918D5-2FFC-4B1D-9AF1-0B66C68F64B2}.xml
Saved file C:\Tasks\win-acme renew (acme-v02.api.letsencrypt.org).xml
Task folder: \Mozilla\
Saved file C:\Tasks\Mozilla\Firefox Background Update 308046B0AF4A39CB.xml
Saved file C:\Tasks\Mozilla\Firefox Default Browser Agent 308046B0AF4A39CB.xml
Exporting of scheduled tasks finished.
Verify Scheduled Tasks XML files
The Export-SchedTasks.ps1 PowerShell script bulk exports all tasks to XML files. Find the XML files in the path C:\Tasks.
Open the XML file with your favorite application. For example, Microsoft Edge, Notepad, or Notepad++.
The XML file looks excellent.
Did this help you to back up the scheduled tasks with PowerShell to XML files?
Read more: Create scheduled task with PowerShell »
Conclusion
You learned how to export scheduled tasks with PowerShell. First, run the Export-SchedTasks PowerShell script. Next, go to the export folder to check all the exported scheduled tasks in XML files. The script works for Windows Server and Windows Clients.
Did you enjoy this article? You may also like Windows Server post-installation configuration. Don’t forget to follow us and share this article.
This Post Has 0 Comments