Monday, May 20, 2024
HomePowershellFind out how to Create a PowerShell Scheduled Activity — LazyAdmin

Find out how to Create a PowerShell Scheduled Activity — LazyAdmin


PowerShell scripts are a good way to automate day by day duties or generate weekly studies. However how do you make a scheduled activity in your PowerShell scripts? We will do that with both the duty scheduler to run a PowerShell script or create the duty in PowerShell.

The benefit of scheduled duties is which you can set and overlook them. They are going to run within the background, and replace your programs, or generate the studies that you just in any other case must do manually.

On this article, we are going to have a look at run a PowerShell script with activity scheduler and create a scheduled activity in PowerShell.

Use Activity Scheduler to Run a PowerShell Script

The primary possibility that we’re going to have a look at is utilizing the duty scheduler in Home windows to run a PowerShell Script. The Home windows Activity Scheduler originates from Home windows 95! and is a good way to schedule duties in Home windows.

To run PowerShell scripts with the duty scheduler, we will’t merely choose the PowerShell file that we need to schedule. What we’re going to do is run the “program” PowerShell and use the arguments to pick the file that we need to run.

Time wanted: 2 minutes

  1. Open Activity Scheduler

    Click on on Begin and sort “Activity scheduler” to open it. Or choose it within the Begin Menu beneath Home windows Administrative Instruments (or Home windows Instruments when utilizing Win 11)

  2. Create a brand new primary activity

    Click on on Create Fundamental Duties… within the motion bar on the proper aspect and provides your activity a reputation.
    (I like to recommend organizing your duties in folders, create them on the left aspect first)

    task scheduler run a powershell script

  3. Schedule the duty

    The set off determines when the duty needs to be executed. Select a set off, for instance, Weekly, and click on Subsequent to configure when precisely the duties have to be executed.

  4. Set the Motion

    Right here comes the essential half, for the motion, we’re going to Begin a Program

    run powershell script on task scheduler

  5. Begin a Program – PowerShell

    This system that we need to run is PowerShell. Simply enter PowerShell within the Program/Script subject (see screenshot in step 6), you don’t want to seek out the precise path to the executable.

  6. Add Arguments

    Within the arguments subject, we’re going to add the argument -File and path to the PowerShell script. It’s additionally a good suggestion so as to add the PowerShell swap -NoProfile and set the -ExecutionPolicy to ByPass.

    In case your scripts require any parameters then you’ll be able to add them after the trail (for instance the -Output parameter):

    -NoProfile -ExecutionPolicy Bypass -File "C:scriptsADHealth.ps1" -Output "HTML"

    task scheduler with powershell

  7. Begin in

    At all times set the Begin in location to the identical path as the place your script is positioned. This fashion any exports that your script generates will probably be saved in the identical location as your script. In any other case, the output will probably be saved in C:WindowsSystem32

  8. End

    Click on on Subsequent and assessment your settings on the end display. Just be sure you choose Open the properties dialog … so we configure some extra settings.

    task scheduler powershell script

  9. Superior Settings

    There are two settings that we have to change for our scheduled PowerShell activity. We might want to guarantee that the script runs even when we’re not logged on, which you do on the Common tab.

    And we need to guarantee that the script stops if it’s working for greater than a day. Change this setting beneath Settings and set Cease the duty if it runs longer than to 1 day or much less.

    run powershell from task scheduler

  10. Save the duty

    Click on Okay and enter your password, so the duty can run if you find yourself not logged on. It’s all the time a good suggestion to check your activity, right-click on it, and select Run to run the duty now.

Use PowerShell to Create Scheduled Activity

We will in fact additionally use PowerShell to create and handle scheduled duties. Making a scheduled activity in PowerShell requires a few steps. For every step, there’s a totally different cmdlet that we’re going to use.

To create the scheduled activity we’re going to use the next cmdlets:

Cmdlet Description
New-ScheduledTaskTrigger Creates a set off object for a scheduled activity
New-ScheduledTaskAction Creates the motion for a scheduled activity
Register-ScheduledTask Registers a scheduled activity
Get-ScheduledTask Present the scheduled duties

To create a scheduled activity in PowerShell we might want to outline the Set off and Motion for the duty a minimum of. So to create a easy activity that runs each Friday and executes a PowerShell script we will do the next:

$taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Friday -At 3am
$taskAction = New-ScheduledTaskAction -Execute "PowerShell" -Argument "-NoProfile -ExecutionPolicy Bypass -File 'C:scriptsADHealth.ps1' -Output 'HTML'" -WorkingDirectory 'c:scripts'

Register-ScheduledTask 'Lazy PS Duties' -Motion $taskAction -Set off $taskTrigger

It will create the very same activity that we’ve got created with the duty scheduler. However let’s clarify every of the cmdlets to see what choices we’ve got.

Creating the Set off – New-ScheduledTaskTrigger

Step one is to outline the set off. This determines after we need to run the duty. Identical to with the duty scheduler, we will outline a number of triggers for our activity.

The New-ScheduledTaskTrigger cmdlet comes with a few parameters that can help you outline whenever you need to activity to run:

  • AtLogOn – Runs the duty when a consumer logs on
  • AtStartup – When the system is began
  • At – Used with As soon as, Every day, or Weekly. Defines the precise time to run the duty
  • Every day – Runs daily. Outline the times with DaysOfWeek
  • DaysInterval – Run each x days
  • DaysOfWeek – This can be utilized with Every day or Weekly. Defines which days to run the duty
  • Weekly – Runs the duties each week
  • WeeklyInterval – Determines the interval between the weeks.

So to run the duty each week on Tuesday and Thursday at 08:30 AM we will create the next set off:

$set off = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Tuesday,Thursday -At 8:30am

Defining the Motion – New-ScheduledTaskAction

To create the scheduled activity motion in PowerShell we might want to outline which program we wish it to run, any arguments (the trail to the PowerShell scripts for instance), and optionally the working listing.

So to run our PowerShell script we are going to set the -Execute parameter to PowerShell and outline the file path and different parameters within the -Argument parameter. In case your scripts require it, you’ll be able to set the –WorkingDirectory parameter to the trail of your script

$motion = New-ScheduledTaskAction -Execute 'PowerShell' -Argument '-NoProfile -ExecutionPolicy Bypass -File "C:scriptsADHealth.ps1" -Output "HTML"'

Creating the Activity – Register-ScheduledTask

With the set off and motion outlined, we will create and register the scheduled activity. We might want to give our activity a reputation and I like to recommend including a TaskPath (folder) as properly. It will make it simpler to retrieve your duties in a while.

Register-ScheduledTask -TaskName 'Lazy PS Duties' -TaskPath 'LazyTasks' -Motion $taskAction -Set off $taskTrigger
PowerShell to Create Scheduled Task

As talked about you’ll be able to outline a number of triggers and actions in your activity. To do that you will have so as to add the set off or actions in an array. For instance, to run the duty each Tuesday and Thursday and on the primary day of the month we will do:

# Outline the triggers for Tuesday and Thursday at 08:30 AM and when a consumer logs on
$taskTriggers = @(
    New-ScheduledTaskTrigger -Weekly -DaysOfWeek Tuesday,Thursday -At 08:30,
    New-ScheduledTaskTrigger -AtLogon
)

Register-ScheduledTask -TaskName 'Lazy PS Duties' -TaskPath 'LazyTasks' -Motion $taskAction -Set off $taskTriggers

Operating the Activity with Totally different Privileges

After we created the PowerShell activity with the Activity Scheduled, we enabled to choice to run the duty whether or not a consumer is logged on or not. For this, we additionally wanted to produce the username and password to run the duty. Additionally, we might want to set the parameter -RunLevel of the duty to highest:

Register-ScheduledTask -TaskName "Lazy PowerShell Duties" -taskPath 'LazyTasks' -Motion $taskAction -Set off $taskTrigger -Person "lazyadminAdministrator" -Password 'yourPass123' -RunLevel Highest

View the Scheduled Activity

You possibly can view your created duties within the activity scheduler. However you may also retrieve the scheduled activity with PowerShell. For this, we might want to use the cmdlet Get-ScheduledTask. For those who run the cmdlet with none parameters, then it should retrieve all scheduled duties in your laptop.

And as you’ll be able to see, that’s quite a bit. That’s why it’s essential to arrange your duties in folders by utilizing the taskPath parameter. This fashion you should utilize the TaskPath parameter to view solely our duties:

Get-ScheduledTask -TaskPath LazyTasks

The duty path all the time begins and ends with a backslash . To view your duties you will have to produce the TaskName. If the title is exclusive, then the title alone will probably be sufficient. In any other case, you will have to produce the trail as properly:

Get-ScheduledTask -TaskName 'Lazy PS Duties' -TaskPath LazyTasks | Choose *

As you’ll be able to see within the screenshot above, we will’t see all the small print of the duty. To view the Actions or Triggers we might want to broaden the properties. We will do that by utilizing the ExpandProperty methodology or by saving the ends in a variable:

Get-ScheduledTask -TaskName 'Lazy PS Duties' -TaskPath LazyTasks | Choose -ExpandProperty Triggers

# Or
$activity = Get-ScheduledTask -TaskName 'Lazy PS Duties' -TaskPath LazyTasks
$activity.Triggers

Updating Duties

To replace scheduled duties with PowerShell you should utilize the Set-ScheduledTask cmdlet. It really works just like the Register-ScheduledTask cmdlet. Simply create a brand new set off or motion first after which add it identical to whenever you would create it.

# Outline new set off
$newTrigger= New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 08:30

# Replace the duty
Set-ScheduledTask -TaskName "Lazy PowerShell Duties" -taskPath 'LazyTasks'-Set off $newTrigger

Wrapping Up

Scheduling PowerShell Duties may be finished with each Activity Scheduler and in PowerShell with the Register-ScheduledTask cmdlet. Personally, I discover it simpler to create the duty utilizing PowerShell, however to change it, I discover the duty scheduler extra handy.

Nonetheless, you should utilize each choices to schedule your PowerShell scripts. I hope you discovered this text useful, in case you have any questions, simply drop a remark under.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments