Windows Tip Sheet

Time Machine

Date-time objects give you a sense of a script's performance.

In PowerShell I'm often wondering how long a particular script or process took to complete. It's is a great way to gauge a script's performance in a non-scientific sort of way. Fortunately, you can do this in PowerShell using Date-time objects. PowerShell is smart enough to be able to subtract two date-time objects. All you need is a beginning time and end time.

In your script or at the console, before you run your lengthy command, run this:


Then, run your command. Or if you just want to see what I'm talking about, wait a few minutes pretending something is running, then type:


The runtime will be the difference between the two. I like to create a variable:


If you look at $runtime, you should see output like this:

Days : 0

Hours : 0

Minutes : 13

Seconds : 24

Milliseconds : 292

Ticks : 8042925552

TotalDays : 0.00930894161111111

TotalHours : 0.223414598666667

TotalMinutes : 13.40487592

TotalSeconds : 804.2925552

TotalMilliseconds : 804292.5552

To display any part of $runtime all you need to do is use a command like:


What I prefer to do in my script or session is to use a single-line expression like this (it's wrapping here, so remember to type this as a single line):

Write-host Process took $Runtime.Days days $Runtime.Hours hours $Runtime.minutes minutes $Runtime.Seconds seconds $runtime.milliseconds milliseconds

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the editors at [email protected]; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

As a faster alternative, you can use this:

$runtime | format-table -auto

Because $runtime is an object, you can do all sorts of things with it, such as checking the number of minutes and, if it exceeds a certain value, display the time in red. I'm sure you'll think of other ways to leverage this information.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular