So you want a quick and easy stopwatch with PowerShell to time how long you’ve been at a task?  Want to see how long it takes for your coworkers to realize you’re awesome?  Would you like to use PowerShell as a timer or alarm clock?   You can easily use PowerShell to keep track of how long an antivirus scan or checkdisk is taking on another system!

PowerShell contains many of the .NET class library by default, but there are thousands more waiting for you to connect them.

The Stopwatch class is available as part of the System.Diagnostics namespace. To gain access to the classes in it you have to load the namespace.

[System.Reflection.Assembly]::LoadWithPartialName(“System.Diagnostics”)

Once loaded, you can create a stopwatch object.

$sw = new-object system.diagnostics.stopwatch

now that you’ve got your stopwatch, you have access to the methods and properties of the stopwatch class:

Method
Usage
Notes
Start $sw.start() Begin the stopwatch counting
Stop $sw.stop() Stops the stopwatch counting
Reset $sw.reset() Stops the counter and sets elapsed to zero

 

Property
Usage
Notes
IsRunning $sw.IsRunning Returns True/False
Elapsed $sw.Elapsed Returns all elapsed information
.Days $sw.elapsed.days Integer (Int32) days
(how many whole days)
.Hours $sw.elapsed.hours Int32 Hours
.Minutes $sw.elapsed.minutes Int32 Minutes
.Seconds $sw.elapsed.seconds Int32 Seconds
.Milliseconds Int32 Milliseconds
.Ticks Int64 Ticks
.TotalDays You get the point
.TotalHours and
.TotalMinutes this table is getting
.TotalSeconds crowded
.TotalMilliseconds
ElapsedTicks $sw.elapsedticks does not require “elapsed.ticks”
ElapsedMilliseconds $sw.elapsedmilliseconds does not require “elapsed.milliseconds”
Excellent, dude.  I now know one more useless thing to do with PowerShell.

Wrong!  you now know 2 useless things to do with PowerShell:

Tracking Time and Setting Alarms

A note about this:  There IS a measure-command cmdlet in Powershell, which is great because it automatically starts the timer when the command starts, and ends it when the command is over.  But there are some drawbacks to using measure-command:

  • Output is suppressed, like this:

get-qaduser domain\me

returns my acct, but:

”measure-command {get-qaduser domain\me }”

returns the time it takes to get my acct. It does not output my user account to the screen.

  • It runs on only one script block.  If you want to time several commands, or check to see how long a whole process takes (especially if you have an external force like a coworker or other department) a measure-command isn’t going to work.

Things you can time: Here is just a small sample of things you can time.

  1. Your coworkers breaks (but not your own, right?)
  2. How long a Chkdisk or Defrag runs on a machine.
  3. How long it takes your coffee go cool off (or your Mountain Dew to warm up – try doing THAT with measure-command.
  4. How long it takes to parse through enormous lists of users/computers/anything else.

How to set an alarm: Want to work on a project for 20 minutes?  Or how bout start playing an annoying song 15 minutes after you leave for lunch?

while ($sw.elapsed.minutes –lt 20) {$null}; invoke-item “C:\users\public\Music\Sample Music\kalimba.mp3”

3 parts really make this work.

1) (elapsed.minutes -lt 20)  –  We identify our alarm time – in this case “don’t do anything until the timer hits 20 minutes”.

2) {$null}  – While we’re waiting we just do nothing.

3) invoke-item c:\music.mp3 – “invoke-item” means open the file with it’s default program – for me it plays in Media Player, or Winamp – depending on what machine I’m on.  

When the music plays, you know the timer is up!

ilovepowershell.com Quick-Commands:

Load the diagnostic objects:
[System.Reflection.Assembly]::LoadWithPartialName(“System.diagnostics”)

Create a stopwatch:
$sw = new-object system.diagnostics.stopwatch

Create an alarm:
while ($sw.elapsed.minutes –lt 20) {$null}; invoke-item “c:\users\public\music\sample music\kalimba.mp3”