Creating Scheduled Task with One-Minute Interval: Tech Support Guide
In this guide, we'll walk you through the process of creating a scheduled task that runs every minute using PowerShell. This technique can be useful for various automation scenarios, such as triggering backups, running scripts, or executing system maintenance tasks at regular intervals.
Prerequisites
To follow this guide, ensure you have the following:
- A Windows operating system (with PowerShell installed)
- Administrative privileges
Step 1: Open PowerShell or Windows Terminal
Press Win + X and choose Windows PowerShell (Admin) or Windows Terminal (Admin) from the menu. This will grant you the necessary permissions to create a scheduled task.
Step 2: Create a PowerShell Script
You can create and save the following PowerShell script as a *.ps1 file:
-$Time = (Get-Date).AddMinutes(1).ToString("HH:mm")
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\path\to\your\script.ps1"
$Trigger = New-ScheduledTaskTrigger -Once -At $Time -RepetitionInterval (new-timespan -Minutes 1)
$Settings = New-ScheduledTaskSettingsSet
$Task = New-ScheduledTask -Action $Action -Principal (New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount) -Trigger $Trigger -Settings $Settings
Register-ScheduledTask -TaskName "OneMinuteTask" -InputObject $TaskReplace C:\path\to\your\script.ps1 with the path to the PowerShell script you want to execute every minute. Note that the script should be located on a local drive, as network paths are not supported in this example.
Step 3: Execute the Script
To run the script, execute the following command in PowerShell:
.\your-script-name.ps1Replace your-script-name.ps1 with the name of the PowerShell script you created in step 2.
Understanding the Code
Let's break down the script and go over the critical components:
-
$Time = (Get-Date).AddMinutes(1).ToString("HH:mm")This line sets the task trigger time to be one minute from the current time. It converts the date to a string format displaying only the hours (HH) and minutes (mm).
-
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\path\to\your\script.ps1"This line creates a new scheduled task action that will execute PowerShell.exe along with the specified script as an argument. Ensure you provide the correct path to your script.
-
$Trigger = New-ScheduledTaskTrigger -Once -At $Time -RepetitionInterval (new-timespan -Minutes 1)This line defines the task trigger. It sets the trigger to run once at the specified time ($Time) and then repeats every one minute.
-
$Settings = New-ScheduledTaskSettingsSetThis line initializes the task settings object, which stores additional parameters not covered in this guide. You can explore other setting options in the official documentation.
-
$Task = New-ScheduledTask -Action $Action -Principal (New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount) -Trigger $Trigger -Settings $SettingsThis line creates the scheduled task object by combining the action, principal (user), trigger, and settings objects.
-
Register-ScheduledTask -TaskName "OneMinuteTask" -InputObject $TaskFinally, this line registers the scheduled task with the Task Scheduler service.
- New-ScheduledTask - A PowerShell cmdlet to create a new scheduled task.
- Register-ScheduledTask - A PowerShell cmdlet to register the scheduled task with the Task Scheduler service.
- Get-ScheduledTask - A PowerShell cmdlet to retrieve a list of scheduled tasks or information about a specific task.