Rather than using the graphical Task Scheduler (taskschd.msc
) snap-in or the legacy schtasks.exe
console command, you can use PowerShell to manage scheduled tasks. This post will cover how to use PowerShell to create, edit, delete, and run Task Scheduler tasks in Windows.
The built-in PowerShell module ScheduledTasks is available in modern versions of Windows for managing tasks in the scheduler. You can list the cmdlets in a module as follows:
Get-Command -Module ScheduledTasks
Creating a Scheduled Task with Windows PowerShell
Now, let’s look at how to create a new scheduler task using the PowerShell command prompt. Suppose you want to create a task in the scheduler that runs during startup (or at a specific time) and runs a PowerShell script. When creating a new task, the following settings must be configured:
- Action – it is an action that performs a task, such as running a command, program, or script. Use the
New-ScheduledTaskAction
cmdlet to create this object. - Trigger – specify when the task should be executed: according to a specific schedule or when an event occurs:
New-ScheduledTaskTrigger
- Principal – The account under which the task is executed:
New-ScheduledTaskPrincipal
First, set the task name:
$NewTaskName = "StartupScript_PS"
Choose the user account under which the task should run. In this example, I’ll launch the script with SYSTEM account privileges.
$User="NT AUTHORITY\SYSTEM"
$User = "woshub\sysops1"
In order to run a task on behalf of a non-admin user, their account must be added to the local policy, “Log on as a batch job“.
You can run tasks using AD-managed service accounts (gMSA).
$svcUserPrincipal = New-ScheduledTaskPrincipal -UserID woshub\monsql01$ -LogonType Password
Then, use the New-ScheduledTaskTrigger command to create a trigger that determines when the task will start. Here you can use system events:
AtLogOn
– when the user logs inAtStartup
– during the computer boot
Or specify the time:
Once
– run task onceAt
– set the exact execution timeDaily
DaysOfWeek
Weekly
— run on certain weeks of the monthWeeksInterval
— interval between weeks
For example, you can schedule a task to run every day at 10:00.
$Trigger = New-ScheduledTaskTrigger -At 10:00am -Daily
- Run at every computer boot:
$Trigger = New-ScheduledTaskTrigger -AtStartup
- Run when the user logs in:
$Trigger = New-ScheduledTaskTrigger –AtLogon
- A one-time run at a specified time:
$Trigger= New-ScheduledTaskTrigger -Once -At 03:00
- Every second Wednesday of the month:
$Trigger = New-ScheduledTaskTrigger -Weekly -WeeksInterval 2 -DaysOfWeek Wednesday -At 01:00
Next, specify the action that the task should perform. In this case, we need to run the specified PowerShell script.
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File C:\PS\StartupScript.ps1"
-Bypass
parameter allows running a PowerShell script on a schedule, regardless of the PowerShell Execution Policy settings.You can now create a task with specified settings.
Register-ScheduledTask -TaskName $NewTaskName -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force
-RunLevel Highest
option used to run the task with elevated permissions (the “Run with highest privileges” checkbox is enabled
).If the task was created successfully, the status Ready appears.
Open the taskschd.msc
snap-in and verify that a new scheduler task has appeared in the Task Scheduler Library.
$TaskName = "NewPsTask"
$TaskDescription = "Running PowerShell script from Task Scheduler"
$TaskCommand = "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
$TaskScript = "C:\PS\StartupScript.ps1"
$TaskArg = "-WindowStyle Hidden -NonInteractive -Executionpolicy unrestricted -file $TaskScript"
$TaskStartTime = [datetime]::Now.AddMinutes(1)
$service = new-object -ComObject("Schedule.Service")
$service.Connect()
$rootFolder = $service.GetFolder("\")
$TaskDefinition = $service.NewTask(0)
$TaskDefinition.RegistrationInfo.Description = "$TaskDescription"
$TaskDefinition.Settings.Enabled = $true
$TaskDefinition.Settings.AllowDemandStart = $true
$triggers = $TaskDefinition.Triggers
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa383915(v=vs.85).aspx
$trigger = $triggers.Create(8)
Change Scheduled Task Settings with PowerShell
Use the Set-ScheduledTask cmdlet to change the settings of an existing scheduler task.
For example, to change the scheduled task runtime (add multiple triggers) or the action performed:
$Trigger1 = New-ScheduledTaskTrigger -At 05:00 -Weekly -DaysOfWeek Monday
$Trigger2= New-ScheduledTaskTrigger -At 05:00 -Weekly -DaysOfWeek Saturday
$action = New-ScheduledTaskAction -Execute "myapp.exe"
Set-ScheduledTask -TaskName StartupScript_PS -Trigger $trigger1, $trigger2 -Action $action
To change the username used to run the task and, for example, the compatibility mode:
$task_user = New-ScheduledTaskPrincipal -UserId 'woshub\j.abrams' -RunLevel Highest
$task_settings = New-ScheduledTaskSettingsSet -Compatibility 'Win8'
Set-ScheduledTask -TaskName StartupScript_PS -Principal $task_user -Settings $task_settings
If you receive the error Set-ScheduledTask : No mapping between account names and security IDs was done, make sure you have specified the correct username.
Managing Scheduled Tasks with PowerShell
To list all active task scheduler tasks in Windows, use the command:
Get-ScheduledTask -TaskPath | ? state -ne Disabled
The command prints the time of the previous task run, the last return error code, and the next run time:
Get-ScheduledTask CheckServiceState| Get-ScheduledTaskInfo
LastRunTime : 4/7/2025 10:00:00 AM LastTaskResult : 267011 NextRunTime : 4/8/2025 10:00:00 AM NumberOfMissedRuns : 0 TaskName : CheckServiceState TaskPath : \ PSComputerName :
List all scheduled task settings.
$task = Get-ScheduledTask StartupScript_PS
$task.Settings
You can disable this task:
Get-ScheduledTask CheckServiceState | Disable-ScheduledTask
To enable a task:
Get-ScheduledTask CheckServiceState | Enable-ScheduledTask
To run the task immediately (without waiting for the scheduled time or event):
Start-ScheduledTask CheckServiceState
To completely remove a task from the Task Scheduler library:
Unregister-ScheduledTask -TaskName CheckServiceState
How to Export/Import Scheduled Tasks with PowerShell
You can use PowerShell to export the scheduler task settings to an XML text file. This is useful when you need to copy configured tasks to other computers or create a backup copy. The task can be exported from both the Task Scheduler GUI and the PowerShell console.
Here is the command to export the task with the name StartupScript to the StartupScript.xml file:
Export-ScheduledTask StartupScript | out-file c:\tmp\StartupScript.xml
To import task settings to another computer, specify the path to the resulting XML file in the Register-ScheduledTask cmdlet parameters:
Register-ScheduledTask -Xml (Get-Content “\\mun-fs01\public\NewPsTask.xml” | out-string) -TaskName "NewPsTask"
2 comments
Hello,
Thx for your job, it helps me very often.
To make a task run at logon of “all users”, here is how to define the principal :
$ALL_USER_SID = New-Object System.Security.Principal.SecurityIdentifier (“S-1-1-0”)
$ALL_USER_GROUP_OBJ = $ALL_USER_SID.Translate( [System.Security.Principal.NTAccount])
$ALL_USER_GROUP_NAME = $ALL_USER_GROUP_OBJ.Value
$PRINCIPAL = New-ScheduledTaskPrincipal -GroupId $ALL_USER_GROUP_NAME
Hope this help 🙂
Great hint!👍