Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
87 lines (70 sloc) 2.08 KB
layout title excerpt permalink tags categories published header gallery toc toc_label
PowerShell - Executing code at specific times
Someone recently asked me how to trigger code at specific times without leveraging the scheduled tasks
teaserlogo teaser image caption
image_path url title
Table of content

Tested on:

  • Windows PowerShell 5.1
  • PowerShell 6.2.2

Quick tip - How to trigger code at specific times

I recently had someone ask how they could trigger a piece of PowerShell code at different times.

This type of tasks can usually be handled by other systems such as Scheduler, Event or message based systems.

In this particular case, it needed to be handled in the current PowerShell session.

I came up with the following solution.

Let me know in the comment section if you would use another approach and I'll add you to the post.

# Specify the Execution times
$TriggerTimes = @(

# Sort in chronologic order
#  assuming the times format are the same
$TriggerTimes = $TriggerTimes | Sort-Object

foreach ($t in $TriggerTimes)
    # Past time ?
    if((Get-Date) -lt (Get-Date -Date $t))
        # Sleeping
        while ((Get-Date -Date $t) -gt (Get-Date))
            # Sleep for the remaining time
            (Get-Date -Date $t) - (Get-Date) | Start-Sleep

        # Trigger event
        #  insert your code here
        "# TriggerTime: '$t' - Executing my code here!"

    }else{"Belong to the past: '$t'"}


PS /home/fx/code> ./triggertimes.ps1

Belong to the past: '1:00:00pm'
Belong to the past: '2:00:00pm'
# TriggerTime: '4:12:00pm' - Executing my code here!
# TriggerTime: '4:12:10pm' - Executing my code here!
# TriggerTime: '4:12:20pm' - Executing my code here!


You can’t perform that action at this time.