EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Scheduled Task

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Scheduled Task

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.

Different Commands to Create a Scheduled Task via PowerShell

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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

Popular Course in this category
Sale
PowerShell Training (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 4+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,985 ratings)
Course Price

View Course

Related Courses
Shell Scripting Training (4 Courses, 1 Project)All in One Data Science Bundle (360+ Courses, 50+ projects)Data Visualization Training (15 Courses, 5+ Projects)

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

PowerShell Training (2 Courses)

2 Online Courses

1 Hands-on Project

4+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • 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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

EDUCBA Login

Forgot Password?

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

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

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

Special Offer - PowerShell Training (2 Courses) Learn More