THE SQL Server Blog Spot on the Web

Welcome to - The SQL Server blog spot on the web Sign in | |
in Search

Jamie Thomson

This is the blog of Jamie Thomson, a data mangler in London working for Dunnhumby

Time your commands [Powershell nugget]

When using Powershell I’ve always found it useful to be able to time how long commands/scripts are running for, invaluable when running long-running scripts. I used to do something similar to this in all my scripts:


which is rather crude. It also has the disadvantage that I have to remember to actually put this into my scripts. What I’d prefer would be a way for Powershell to always tell me how long it takes my commands/scripts to run and thanks to my old Conchango colleague Stuart Preston I now have a way. Using Stuart’s code that he has put up in a gist one can have Powershell output this information every time a command is executed:


Note that the current location is still present in the title bar (Stuart’s code does that too) and time elapsed since the last command is displayed. Perfect! Simply put Stuart’s code into your profile and you’re done.

A simple little trick but one which I think its worth calling out in a blog post!


Published Tuesday, August 26, 2014 3:04 PM by jamiet
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS



Andrew said:

I neat idea, but I found that the code doesn't work. It was just subtracting seconds for me.

Some quick reading landed me at something that I think works much better and is a little more flexible, so this is more or less what I ended up using:

$startDate = Get-Date;

Start-Sleep -s 3; #Wait 3 seconds for demo purposes

Write-Host "Time Taken: ", (New-TimeSpan -Start $startDate).Seconds, "seconds" -ForegroundColor Green;

August 27, 2014 5:50 PM

Andrew said:

...I made a mistake in my last comment. Using TotalSeconds is the way to go:

Write-Host "Time Taken:", (New-TimeSpan -Start $startDate).TotalSeconds, "seconds" -ForegroundColor Green;

August 27, 2014 6:08 PM

Leave a Comment


This Blog


Privacy Statement