EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Scheduled Task

PowerShell Scheduled Task

Priya Pedamkar
Article byPriya Pedamkar

Updated March 27, 2023

PowerShell Scheduled Task

Introduction to PowerShell Scheduled Task

Scheduled tasks are nothing but operations that runs automatically to perform some activity on a regular basis without manual intervention. The activity can be anything from rebooting a system, running a batch command or running a PowerShell script. The common and monotonous tasks are the ones that are updated or achieved via a scheduled task. This helps in reducing the man-hours spent. The scheduled task can be made to run every hour or day or weekly depending on the need of the user. This article will cover in detail how a scheduled task can be created to run a PowerShell script through a task scheduler and through PowerShell.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Different Commands to Create a Scheduled Task via PowerShell

The following are the cmdlets that are required to create a scheduled task via PowerShell:

  • New-ScheduledTask
  • New-ScheduledTaskAction
  • New-ScheduledTaskTrigger
  • New-ScheduledTaskSettingsSet
  • Register-ScheduledTask

Let’s see in detail each of the above-mentioned cmdlets.

1. New-ScheduledTask

This cmdlet creates a new scheduled task job instance.

Syntax:

NAME
New-ScheduledTask
SYNTAX
New-ScheduledTask [[-Action] <CimInstance#MSFT_TaskAction[]>] [[-Trigger] <CimInstance#MSFT_TaskTrigger[]>] [[-Settings] <CimInstance#MSFT_TaskSettings>] [[-Principal] <CimInstance#MSFT_TaskPrincipal>] [[-Description] <string>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob]  [<CommonParameters>] ALIASES
None

Parameters:

  • Action: It denotes the action that needs to be performed by the task. It can be multiple actions, in case of multiple actions specified they are run sequentially. The maximum number of actions specified is 32. Its type is CimInstance[]. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • AsJob: This denotes the task to be run as a background job. Its type is the switch. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • CimSession: This runs the job on a remote computer. Its type is CimSession[] and alias is session. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • Description: This denotes the basic definition of the task. Its type is a string and the default value is none. It doesn’t accept pipeline input and wildcard characters.
  • Principal: It denotes the user account that needs to be used by the task. Its type is CimInstance. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • Trigger: This denotes the trigger which will start the scheduled task. The trigger can either be a time-based one or an event-based one. A task can have a maximum of 48 triggers. Its type is CimInstance[]. The default value is none. It doesn’t accept pipeline input and wildcard characters.

Example:

$Tasktest = New-ScheduledTask -Action $testAction -Trigger $testTrigger -Settings $testSettings

2. New-ScheduledTaskAction

This cmdlet is used to create the action of a scheduled task.

Syntax:

NAME
New-ScheduledTaskAction
SYNTAX
New-ScheduledTaskAction [-Execute] <string> [[-Argument] <string>] [[-WorkingDirectory] <string>] [-Id <string>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob]  [<CommonParameters>] ALIASES
None

Parameters:

  • Argument: It specifies the list of arguments to be passed. Its type is a string. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • AsJob: This denotes the task to be run as a background job. Its type is a switch. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • Execute: It denotes the path of the file to be executed. Its type is a string. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • Id: It denotes the action id and it is used for logging. Its type is a string. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • WorkingDirectory: It denotes the directory in which the task scheduler will run. The default location is the %windir%\system32 directory. The default value is none. It doesn’t accept pipeline input and wildcard characters.

Example:

New-ScheduledTaskAction -Execute "Notepad.exe"

3. New-ScheduledTaskTrigger

This creates a trigger for the scheduled task. The trigger can be time-based or event-based.

Syntax:

NAME
New-ScheduledTaskTrigger
SYNTAX
New-ScheduledTaskTrigger [-Once] -At <datetime> [-RandomDelay <timespan>] [-RepetitionDuration <timespan>] [-RepetitionInterval <timespan>] [-CimSession
<CimSession[]>] [-ThrottleLimit <int>] [-AsJob]  [<CommonParameters>] New-ScheduledTaskTrigger [-Daily] -At <datetime> [-DaysInterval <uint32>] [-RandomDelay <timespan>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob] [<CommonParameters>] New-ScheduledTaskTrigger [-Weekly] -At <datetime> [-RandomDelay <timespan>] [-DaysOfWeek {Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday}] [-WeeksInterval <uint32>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob]  [<CommonParameters>] New-ScheduledTaskTrigger [-AtStartup] [-RandomDelay <timespan>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob]  [<CommonParameters>] New-ScheduledTaskTrigger [-AtLogOn] [-RandomDelay <timespan>] [-User <string>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob]  [<CommonParameters>] ALIASES
None

Parameters:

  • At: Specifies the date or time at which the task must be triggered. Its type is DateTime. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • AtLogOn: It denotes the trigger to start when a user logs on. Its type is the switch parameter. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • Daily: It denotes the task must be run daily. Its type is the switch parameter. The default value is none. It doesn’t accept pipeline input and wildcard characters.
  • Weekly: It denotes the task must be run weekly. Its type is a switch parameter. The default value is none. It doesn’t accept pipeline input and wildcard characters.

Example:

$Sta = New-ScheduledTaskAction -Execute "Notepad"
$Stestjob = New-ScheduledTaskTrigger -Weekly -At 3 am

4. New-ScheduledTaskSettingsSet

This contains the settings of the scheduled task. This object is used to modify the settings and manage the behavior of the task.

Syntax:

NAME
New-ScheduledTaskSettingsSet
SYNTAX
New-ScheduledTaskSettingsSet [-DisallowDemandStart] [-DisallowHardTerminate] [-Compatibility {At | V1 | Vista | Win7 | Win8}] [-DeleteExpiredTaskAfter <timespan>] [-AllowStartIfOnBatteries] [-Disable] [-MaintenanceExclusive] [-Hidden] [-RunOnlyIfIdle] [-IdleWaitTimeout <timespan>] [-NetworkId <string>] [-NetworkName <string>] [-DisallowStartOnRemoteAppSession] [-MaintenancePeriod <timespan>] [-MaintenanceDeadline <timespan>] [-StartWhenAvailable] [-DontStopIfGoingOnBatteries] [-WakeToRun] [-IdleDuration <timespan>] [-RestartOnIdle] [-DontStopOnIdleEnd] [-ExecutionTimeLimit <timespan>] [-MultipleInstances {Parallel | Queue | IgnoreNew}] [-Priority <int>] [-RestartCount <int>] [-RestartInterval <timespan>] [-RunOnlyIfNetworkAvailable] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob]  [<CommonParameters>] ALIASES
None

Example:

$Statest = New-ScheduledTaskAction -Execute "Notepad"
$STSettest = New-ScheduledTaskSettingsSet -Priority 7

5. Register-ScheduledTask

This is used to register the scheduled task. The following types of applications can be run using the script. Windows applications, DOS applications, batch and cmd files.

Example:

Register-ScheduledTask -TaskName "test task" -Trigger $testTime -User $trestUser -Action $testPS

Sample script to Create a Task

Let us create a task with the help of sample code:

Code:

Write-Host "Task Scheduler Creation"
Write-Host "Specifying the action"
$testAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-File 'C:\test.ps1'"
Write-Host "Setting the trigger"
$testTrigger = New-ScheduledTaskTrigger -Daily -At 4am
$testSettings = New-ScheduledTaskSettingsSet
Write-Host "Creating the task"
$testTask = New-ScheduledTask -Action $testAction -Trigger $testTrigger -Settings $testSettings
Write-Host "registering the task"
Register-ScheduledTask -TaskName 'Dummy TS' -InputObject $Task -User 'vignesh' -Password 'pass@123'
Write-Host "Task Created"

Output:

Powershell scheuled1

Powershell scheuled2

Running a script via Task Scheduler

1. Open task scheduler by searching in windows and select create a basic task. Give a name to the task and select ok

2. Set the triggers, it can be weekly, daily or monthly

3. Select the file to be run on the action tab

4. Pass the arguments if any that is required for the script in the set Argument tab

5. Save the task

6. The task will be available in the task scheduler library.

Conclusion

Thus, the article covered in detail how a scheduled task can be created and configured in PowerShell. It explained in detail the various cmdlets that are involved in creating the task, their syntax and respective parameters along with suitable examples. It also covered how a task can be created directly in the task scheduler. The best way to learn more will be to create a task scheduler using the above-mentioned steps and exploring on our own.

Recommended Articles

This is a guide to PowerShell Scheduled Task. Here we discuss the introduction to PowerShell Scheduled Task, different commands to create a scheduled task via PowerShell along with respective syntax and it’s Parameters. You can also go through our other related articles to learn more –

  1. PowerShell Import-Module
  2. PowerShell Invoke-Command
  3. PowerShell Tools
  4. PowerShell Versions
  5. Guide to PowerShell Convert to String
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle2000+ Hour of HD Videos | 80 Learning Paths | 400+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more