THE SQL Server Blog Spot on the Web

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

Jamie Thomson

This is the blog of Jamie Thomson, a freelance data mangler in London

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:

image

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:

image

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!

@Jamiet

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

Comments

 

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

(required) 
(required) 
Submit

This Blog

Syndication

Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement