Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • 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
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • 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
  • PowerShell
  • Linux

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

March 17, 2024 PowerShellWindows Server 2016

How to Create and Manage Scheduled Tasks with PowerShell

Most users and administrators use the taskschd.msc graphical interface console to create and manage scheduled tasks on Windows. However, in various scripts and automated flows, it is much more convenient to use the PowerShell features to create scheduled tasks. In this article, we’ll show how to create and manage Windows Scheduler tasks using PowerShell.

Contents:
  • Managing Scheduled Tasks on Windows via PowerShell
  • Creating Scheduled Task with Windows PowerShell
  • How to View and Run Scheduled Tasks with PowerShell?
  • How to Export and Import Scheduled Tasks via XML Files?

Managing Scheduled Tasks on Windows via PowerShell

The ScheduledTasks PowerShell module is used to manage scheduled tasks on Windows 10/Windows Server 2016. You can list the cmdlets in a module as follows:

Get-Command -Module ScheduledTasks

  • Disable-ScheduledTask
  • Enable-ScheduledTask
  • Export-ScheduledTask
  • Get-ClusteredScheduledTask
  • Get-ScheduledTask
  • Get-ScheduledTaskInfo
  • New-ScheduledTask
  • New-ScheduledTaskAction
  • New-ScheduledTaskPrincipal
  • New-ScheduledTaskSettingsSet
  • New-ScheduledTaskTrigger
  • Register-ClusteredScheduledTask
  • Register-ScheduledTask
  • Set-ClusteredScheduledTask
  • Set-ScheduledTask
  • Start-ScheduledTask
  • Stop-ScheduledTask
  • Unregister-ClusteredScheduledTask
  • Unregister-ScheduledTask

managing Scheduled Tasks via powershell

Hint. Previously, the built-in console tool schtasks.exe was used in Windows to create and manage scheduler jobs.

Creating Scheduled Task with Windows PowerShell

In PowerShell 3.0 (appeared on Windows Server 2012/Windows 8), you can use the New-ScheduledTaskTrigger and Register-ScheduledTask cmdlets to create scheduled tasks.

Suppose, we need to create a scheduled task that should run during startup (or at a specific time) and execute some PowerShell script or command. Let’s create a scheduled task named StartupScript1. This task should run the PowerShell script file C:\PS\StartupScript.ps1 at 10:00 AM every day. The task will be executed with elevated privileges (checkbox “Run with highest privileges”) under the SYSTEM account.

$Trigger= New-ScheduledTaskTrigger -At 10:00am -Daily
$User= "NT AUTHORITY\SYSTEM"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "C:\PS\StartupScript1.ps1"
Register-ScheduledTask -TaskName "StartupScript1" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force

If the task was created successfully, the status “Ready” appears.

create sheduled task with PowerShell cmdlet Register-ScheduledTask

Your PowerShell script will run on the specified schedule. If you have a PowerShell Execution Policy enabled on your computer that prevents PS1 scripts from executing, you can run a PowerShell script from a scheduled task with the –Bypass parameter.

Use this code when creating a new task:

$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument “-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File C:\PS\StartupScript.ps1"

Tip. If you want the task to run every time during the computer startup, the first command has to be as follows:
$Trigger= New-ScheduledTaskTrigger -AtStartup
If you want to run a task when a user logs on:
$Trigger= New-ScheduledTaskTrigger -AtLogon

Open the taskschd.msc console and make sure you have a new scheduler task 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 can use the Schedule.Service COM interface (or update the PowerShell version). In this example, we create a scheduled task that will execute the specific file containing PowerShell script during startup. The task is performed with the NT AUTHORITY\SYSTEM privileges.

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

How to View and Run Scheduled Tasks with PowerShell?

You can list all active scheduled tasks on Windows with the command:

Get-ScheduledTask -TaskPath | ? state -ne Disabled

To get information about a specific task:

Get-ScheduledTask CheckServiceState| Get-ScheduledTaskInfo

LastRunTime : 4/7/2021 10:00:00 AM
LastTaskResult : 267011
NextRunTime : 4/8/2021 10:00:00 AM
NumberOfMissedRuns : 0
TaskName : CheckServiceState
TaskPath : \
PSComputerName :

Get-ScheduledTaskInfo powershell

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 schedule), run:

Start-ScheduledTask CheckServiceState

disable/enable/start sheduled task manually

To completely remove a task from the Task Scheduler library:

Unregister-ScheduledTask -TaskName CheckServiceState

If you need to change the username from which the task is launched and, for example, the compatibility mode, use the Set-ScheduledTask cmdlet:

$task_user = New-ScheduledTaskPrincipal -UserId woshub\j.abrams' -RunLevel Highest
$task_settings = New-ScheduledTaskSettingsSet -Compatibility 'Win8'
Set-ScheduledTask -TaskName CheckServiceState_PS -Principal $task_user -Settings $task_settings

If you receive the error “Set-ScheduledTask: No mapping between account names and security IDs was done” check that you provide the correct username.

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

How to Export and Import Scheduled Tasks via XML Files?

PowerShell allows you to export the current settings of any scheduled task into a text XML file. So you can export the parameters of any task and deploy a task to other computers. The task may be exported both from the Task Scheduler GUI and from the PowerShell console.

Here is the command to export the task with the name StartupScript to the file StartupScript.xml:

Export-ScheduledTask StartupScript | out-file c:\tmp\StartupScript.xml

Export-ScheduledTask - xml task

The Export-ScheduledTask cmdlet is not available in PowerShell 2.0, so in Windows 7 / Windows Server 2008 R2 it’s better to use the built-in tool schtasks to export the task settings and redirect the result into a text file:

schtasks /query /tn "NewPsTask" /xml >> "c:\tmp\NewPsTask.xml"

After the scheduled task settings are exported to the XML file, it can be imported to any network computer using the GUI, SchTasks.exe, or PowerShell.

Register-ScheduledTask cmdlet can help you to import task settings from an XML file and register it:
Register-ScheduledTask -Xml (Get-Content “\\mun-fs01\public\NewPsTask.xml” | out-string) -TaskName "NewPsTask"

In PowerShell 2.0 (Windows 7/Server 2008 R2), it is easier to import a task using the schtasks tool. The first command creates a new task. The second will run it immediately (without waiting for the event trigger to activate it).

schtasks /create /tn "NewPsTask" /xml "\\Srv1\public\NewPsTask.xml" /ru corp\skrutapal /rp Pa$$w0rd
schtasks /Run /TN "NewPsTask"

Please, note that this example uses the credentials of the account that is used to run the task. If the credentials are not specified, because they are not stored in the job, they will be requested when importing.

2 comments
5
Facebook Twitter Google + Pinterest
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

View Windows Update History with PowerShell (CMD)

April 30, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

Allowing Ping (ICMP Echo) Responses in Windows Firewall

April 15, 2025

How to Pause (Delay) Update Installation on Windows...

April 11, 2025

How to Write Logs to the Windows Event...

March 3, 2025

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!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 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
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • 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