Windows OS Hub
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / How to Create and Manage Scheduled Tasks with PowerShell

September 5, 2025

How to Create and Manage Scheduled Tasks with PowerShell

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.

Contents:
  • Creating a Scheduled Task with Windows PowerShell
  • Change Scheduled Task Settings with PowerShell
  • Managing Scheduled Tasks with PowerShell
  • How to Export/Import Scheduled Tasks with PowerShell

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

managing Scheduled Tasks via powershell

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"

To run a task from a specific user, enter the user’s name in the following format:

$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 in
  • AtStartup – during the computer boot

Or specify the time:

  • Once – run task once
  • At – set the exact execution time
  • Daily
  • DaysOfWeek
  • Weekly — run on certain weeks of the month
  • WeeksInterval — interval between weeks

For example, you can schedule a task to run every day at 10:00.

$Trigger = New-ScheduledTaskTrigger -At 10:00am -Daily

Tip. Other schedule option examples:

  • 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"

In this case, the -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

The -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.

create sheduled task with PowerShell cmdlet Register-ScheduledTask

Open the taskschd.msc snap-in and verify that a new scheduler task has appeared in the Task Scheduler Library.

new task appears in the task sheduler console

In PowerShell 2.0 (Windows 7, Windows Server 2008 R2), to create a scheduled task from PowerShell, you must use the Schedule.Service COM interface (or update the PowerShell version). In this example, we create a scheduled task that will run the PowerShell script file during startup.

$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.

Set-ScheduledTask: No mapping between account names and security IDs was done

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 :

Get-ScheduledTaskInfo powershell

List all scheduled task settings.

$task = Get-ScheduledTask StartupScript_PS
$task.Settings

list sheduled 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

disable/enable/start sheduled task manually

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

Export-ScheduledTask - xml task

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"

In an Active Directory domain environment, configured scheduled tasks can be deployed to computers via Group Policy.

2 comments
6
Facebook Twitter Google + Pinterest
PowerShellWindows 11Windows Server 2022
previous post
Hosting Multiple Websites in IIS with Same Port and IP Address
next post
Auto Lock Computer Screen After Inactivity with GPO

Related Reading

PowerShell: Get Folder Size on Windows

April 2, 2024

How to Download Offline Installer (APPX/MSIX) for Microsoft...

March 12, 2024

Protecting Remote Desktop (RDP) Host from Brute Force...

February 5, 2024

How to Backup and Restore Websites and IIS...

June 8, 2023

Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)

March 17, 2024

How to Refresh (Update) Group Policy Settings on...

August 13, 2024

Slow Access to Shared Folders and Network Drives...

March 11, 2024

How to Uninstall Built-in UWP (APPX) Apps on...

June 6, 2024

2 comments

DjiPih August 7, 2024 - 4:21 pm

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 🙂

Reply
admin August 9, 2024 - 1:09 pm

Great hint!👍

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Fix: Slow Startup of PowerShell Console and Scripts

    September 3, 2025
  • DPI Scaling and Font Size in RDP (RDS) Session

    August 27, 2025
  • Proxmox: Share a Host Directory with VMs via VirtioFS

    August 18, 2025
  • How to Find AD Users with Blank Passwords (Password-Not-Required)

    July 24, 2025
  • Run Elevated Commands with Sudo on Windows 11

    July 16, 2025
  • Find a Process Causing High Disk Usage on Windows

    July 15, 2025
  • Fix: Microsoft Defender Not Updating Automatically in Windows

    July 8, 2025
  • Create a Windows Server VM on Proxmox (Step-by-Step)

    July 7, 2025
  • How to Detect Which User Installed or Removed a Program on Windows

    June 23, 2025
  • Encrypt Any Client-Server App Traffic on Windows with Stunnel

    June 12, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)
  • How to Download Offline Installer (APPX/MSIX) for Microsoft Store App
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • Adding Drivers into VMWare ESXi Installation Image
  • Tracking and Analyzing Remote Desktop Connection Logs in Windows
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top