Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: Timing is Everything

Posted on July 8, 2016

For today's fun I want to introduce you to a PowerShell project I've been working on. As with many of these Friday Fun projects this is something that is hardly groundbreaking but it could be fun to use and hopefully serves an educational purpose.  What I have is a module called MyTimer that contains several commands designed to work with a very simple timer. In fact it is so simple you'll probably think I'm joking.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The commands are based on an extremely simple principal: how much time has elapsed between two events? You can easily do this now with New-Timespan or simply subtracting one datetime from another. My module takes the simplest approach possible: save the current date and time to a variable and when you are ready, calculate a timespan from that variable. Even though the commands reference a "timer" object there really isn't anything fancy or complicated. It is simply a variable that you can name, that has a datetime value.

Start-MyTimer -Name T1

There's no .NET magic or anything complicated. The module commands are designed to make it easier to manage all of this. You can even create multiple timers at the same time in case you want to stop them at different intervals.

Start-MyTimer -Name T2,T3

If you forget what names you've given your timers, which I did, you can use the Find-MyTimer command.

Find my PowerShell timer variables

When you are ready to stop a timer, I bet you can guess what command to use.

Stopping a timer

All the command did was find the variable and calculate a timespan. To make things easy I even added a parameter to get the value as a string.

Getting time result as a string

When you stop a timer, the original variable is removed.

Finally, I also included a command called Get-MyTimer which will show you the current elapsed time, but doesn't "stop" it. This works nicely with Find-MyTimer.

Getting the current state of the "timer"

Because this is so simple you get your prompt back immediately and it doesn't interfere with the rest of your work. Say you get a phone call from an annoying co-worker. You can kick off a timer and when you hang up 90 minutes later you can stop the timer and record exactly how long you were occupied.

Another option is that you could export your timer variables to a file and in a later PowerShell session re-import them. This could be handy when tracking something that might last for days.  In fact, I may have to think about this as an enhancement.

The module is available now on GitHub at https://github.com/jdhitsolutions/MyTimer. I'd love for you to try it out and let me know what you think. Post bugs or requests on GitHub. Eventually I'll probably publish this to the PowerShell Gallery.

But for now, have fun and enjoy!


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d