Run tasks on timers in PowerShell

HomeOther ContentRun tasks on timers in PowerShell
Run tasks on timers in PowerShell
Run tasks on timers in PowerShell
You can use PowerShell to run tasks on periodic timers. For example, let's say you want to clean a folder containing old log files every hour. Instead of using your operating system's task scheduler (i.e. systemd on Linux, MacOS, or Windows Task Scheduler), you can use PowerShell to create a timer.

The .NET Framework has a class named Timer in the System.Timers namespace, defined in System.ComponentModel.TypeConverter.dll. There are two constructors for the Timer class, one with zero parameters and one that accepts a single "double/" value as input. The value /"double/" is the interval, in milliseconds, over which the timer will run.

Once you've created a Timer object, you use the Register-ObjectEvent command to register for the .NET /"Elapsed/" event. On this same command, the -Action parameter allows you to specify a PowerShell ScriptBlock (aka anonymous function) that will be executed every time the Timer runs out.

Producer: Trevor Sullivan
Website: https://trevorsullivan.net
Patreon: https://patreon.com/trevorsullivan
Twitter: https://twitter.com/pcgeek86
LinkedIn: https://www.linkedin.com/in/trevor-sullivan/

#Microsoft #PowerShell #Developer

Please take the opportunity to connect and share this video with your friends and family if you find it useful.

No Comments

Leave a Reply

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