This week, James Brundage posted article named Two Ways to Time PowerShell Commands. There was mentioned very neat trick to check running time of last command. Please read that article first to understand what I am talking about.

I created a function to have easier way to call that command. It’s not so hard, so here is it:

function Get-HistoryItemRunningTime
{
<#
.SYNOPSIS
Displays running for given item from history

.DESCRIPTION
For given history ID (or last command) displays running time.

.PARAMETER Id
Specifies history ID to be used.

.INPUTS
None. You cannot pipe objects to Get-HistoryItemRunningTime.

.OUTPUTS
System.TimeSpan.

.EXAMPLE
C:\PS> Get-HistoryItemRunningTime 3

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 578
Ticks             : 5781250
TotalDays         : 6.69126157407407E-06
TotalHours        : 0.000160590277777778
TotalMinutes      : 0.00963541666666667
TotalSeconds      : 0.578125
TotalMilliseconds : 578.125

Displays running time for command ID #3.

.EXAMPLE
PS C:\> Start-Sleep 2
PS C:\> Get-HistoryItemRunningTime

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 2
Milliseconds      : 15
Ticks             : 20156250
TotalDays         : 2.33289930555556E-05
TotalHours        : 0.000559895833333333
TotalMinutes      : 0.03359375
TotalSeconds      : 2.015625
TotalMilliseconds : 2015.625

Displays running time for last command.

.LINK
Get-History
#>     

    param (
        [int64]$Id
    )

    if ($Id)
    {
        $history = Get-History -Id $Id
    }
    else
    {
        $history = Get-History -Count 1
    }

    return ($history.EndExecutionTime - $history.StartExecutionTime)

}

I then decided to make it a bit more useful. What to put it to profile? Hmm. So I modified also my prompt function.

Note: I have fully customized prompt in my profile. In following example I just show the idea, not my current prompt.

$OrigTitle = $host.UI.RawUI.WindowTitle
$ShowRunningTime = $false

function prompt
{
  if($ShowRunningTime)
   {
      $host.UI.RawUI.WindowTitle = "$OrigTitle Time: $(Get-HistoryItemRunningTime)"
   }
   else
   {
    $host.UI.RawUI.WindowTitle = $OrigTitle
   }
}

So now I can run it this way

Get-HistoryItemRunningTime

and if you don’t want to see the time in title, you can just run it for specific history item (please note that ghit is an alias for Get-HistoryItemRunningTime).

PS> h
Id CommandLine
– ———–
1 ghit
2 Start-Sleep 2
3 gps | Out-Null
4 get-date
PS> ghit 2
 
Days : 0
Hours : 0
Minutes : 0
Seconds : 2
Milliseconds : 15
Ticks : 20156250
TotalDays : 2.33289930555556E-05
TotalHours : 0.000559895833333333
TotalMinutes : 0.03359375
TotalSeconds : 2.015625
TotalMilliseconds : 2015.625
 
PS> ghit
 
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 62
Ticks : 625000
TotalDays : 7.2337962962963E-07
TotalHours : 1.73611111111111E-05
TotalMinutes : 0.00104166666666667
TotalSeconds : 0.0625
TotalMilliseconds : 62.5

Thank you, James, for nice trick :)