in

WindowsWindows Loving ItLoving It

[WORKING] PS Tasks Run Everyday | Every minute | Every User Logon

Run PowerShell script Every day or Every Minute or on Every User Logon using Scheduled tasks. Use simple trigger examples to run scripts automatically!

PowerShell Scheduled task

In this post, we are going to see how to create a scheduled task using PowerShell.  We will look into three important use cases.  The first use case is to run the scheduled task every day at a certain time, the second case is to run the task on a specific trigger, and the final use case is to run the task periodically every minute.

We use PowerShell for the following reasons:

  • To execute some instructions and get the output.
  • Write Windows .NET programs without having to compile code.
  • Create complicated programs on the go.

The above reasons are enough to reuse the written script over and over again.  But wouldn’t it be more convenient if we can automate the process of using the script again and again when we want.  Here is where Scheduled tasks come in.

The reasons for using Scheduled tasks are:

  • Execute the PowerShell script regularly without the user prompt.
  • Automate script execution.
  • Streamline the user workflow.

Now that we have seen the important details that we need to know before using PowerShell scheduled tasks, let us see how to create Scheduled tasks using PowerShell.

Jump to:

People also search for:

PowerShell Scheduled task that run Every Day:

  • Run PowerShell Scheduled-task every day at a specified time.
  • PowerShell Scheduled task daily.
  • Register-ScheduledTask and run on a daily basis.

To begin with, we will create a basic scheduled task that runs every day at a specified time.  This task will be scheduled to execute at 9 pm every day.  Thus the user will not have to prompt the script every time at the specified time.

The Scheduled Task is created in three parts.

  • The first part is action, here we specify what needs to be done.
  • The second part is the trigger, in the trigger, we have to specify the condition when the task is executed.
  • Finally, the third part is the most important part, here we register the scheduled task in the task scheduler.  Keep in mind that only the registered tasks are executed.

For the action, we are choosing the script that moves the files created on the same day to the same folder.  The script is as shown below.

$ln = “”
$path = “E:\96\Images\lightshot_script”
dir $path -file | sort creationtime | %{
$ct = $_.creationtime
$fn = “{0}_{1}_{2}” -f $ct.month, $ct.day, $ct.year
If($ln -ne $fn)
{
md -force (Join-Path $path -ChildPath $fn)
$ln = $fn
}
mv $_.fullname -destination (Join-Path $path -ChildPath $ln)
$fn
}

Do not forget to change the path while executing it by yourself.  We can use the above script and build the script needed for registering the scheduled task.  The above given script will go inside the action block of the scheduled task.

$actionMain = New-ScheduledTaskAction -Execute ‘Powershell.exe’ ` -Argument ‘-NoProfile -WindowStyle Hidden -command “& {
Cd “E:\96\Images\lightshot“;
Dir -file | sort creationtime | %{
$ct = $_.creationtime
$fn = “{0}_{1}_{2}” -f $ct.month, $ct.day, $ct.year
$desti = (Join-Path “E:\96\Images\lightshot” -ChildPath $fn)
$desti
md $desti -ErrorAction silent
Mv $_.fullname -destination $desti
}
}”‘

The above script represents the action for the scheduled task.

action script block
action script block

We will create a trigger such that the action will be performed at a specified time.

$trigger = New-ScheduledTaskTrigger -Daily -At 5pm

Here we can see that the action must be performed at 5 PM.

trigger script block
trigger script block

Finally, we will use the $action variable and $trigger variable and register it in the task scheduler with the appropriate name and description.

Register-ScheduledTask -Action $actionMain -Trigger $trigger -TaskName “File Segregator” -Description “Move files created on the same day to same folder”

register the schedule task
register the schedule task

We have the necessary script with us but we have not used the script yet.  To use the script follow the below instruction.

  • Press the start button.
Press the start button
Press the start button
  • Enter PowerShell.
Enter PowerShell
Enter PowerShell
  • Right-click and select, run as administrator.
Run PowerShell with admin privileges
Run PowerShell with admin privileges
  • It will open PowerShell with administrator privileges.
  • We have to enter the script in PowerShell.

We can see the scheduled task in the TaskScheduler.  It is shown in the image.

File segregate
File segregate

PowerShell Schedule task that runs Periodically:

  • Run Powershell scheduled tasks again and again.
  • Run PowerShell scheduled task after time interval.
  • PowerShell Scheduled task -RepetitionInterval example

To run the PowerShell scheduled task periodically, we need to carefully create the triggers.

  • Carefully create the triggers.
  • Register it in the Task Scheduler.

For our example, we will use the same action block used earlier.  But we will use a new trigger definition with a new trigger name.  The trigger is given below.

$triggerLogonMinute = New-ScheduledTaskTrigger -RandomDelay (New-TimeSpan -Minutes 0) -At (Get-Date -Year 2020 -Month 1 -Day 30) -Once -RepetitionDuration (New-TimeSpan -Hours 24) -RepetitionInterval (New-TimeSpan -Minutes 1)-ThrottleLimit 1

Note that,

  • The trigger will start after a random delay of 0 seconds.
  • The trigger will start on 30th Jan 2020 and today we are well past that.
  • Once started, it will run once.
  • The repetition duration is 24 hours, which means, after 24 hours, the task will no longer repeat.
  • The throttle limit set to 1 tells windows that it should run only 1 task at a time.  This prevents a new task from starting if the previous task is not over executing.

We will register the new scheduled task using the below script.

Register-ScheduledTask -Action $actionMain -Trigger $triggerLogonMinute -TaskName “File Segregator Minute” -Description “Move files created on the same day to same folder every minute”

register scheduled task every minute
register scheduled task every minute

We can see the scheduled task in the Task Scheduler.

schedule task every minute
schedule task every minute

PowerShell Scheduled task at Logon:

  • How to run PowerShell task at logon.
  • PowerShell tasks New-ScheduledTaskTrigger -atlogon

Just like running the Scheduled task every minute, we need to create a trigger that will make the scheduled task run at logon.

The script for triggering the scheduled task at logon is very simple and it is given below.

$triggerMain = New-ScheduledTaskTrigger -atlogon

trigger at logon
trigger at logon

We can modify the register scheduled task and register the task.

Register-ScheduledTask -Action $actionMain -Trigger $triggerMain -TaskName “Light File Segregator” -Description “Move screen shots created on the same day to same folder at logon”

register the task
register the task

The registered scheduled task is shown below.

logon scheduled task
logon scheduled task

Verdict:

In this post, we learned how to create a scheduled task that can run at a specified time, run periodically, and run at logon. If you want to know more about PowerShell Scheduled task, please let us know in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *