Skip to content

Instantly share code, notes, and snippets.

@gravejester
Created April 21, 2015 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravejester/4cc3e6533637563eb8ec to your computer and use it in GitHub Desktop.
Save gravejester/4cc3e6533637563eb8ec to your computer and use it in GitHub Desktop.
function Stop-Timer {
<#
.SYNOPSIS
Used together with Start-Timer to get a timespan between to instances and display in a human readable format.
.EXAMPLE
Stop-Timer
.EXAMPLE
Write-Verbose "Runtime: $(Stop-Timer)."
.NOTES
Author: Øyvind Kallstad
Date: 19.04.2015
Version: 1.0
#>
[CmdletBinding()]
param ()
$timerEnd = Get-Date
if (Test-Path variable:\timerStart) {
$timerResult = $timerEnd - $global:timerStart
$resultString = $null
switch ($res) {
{$timerResult.Days -gt 0} {$resultString += ", $($timerResult.Hours) day$(@{$true = 's'}[$timerResult.Days -gt 1])"}
{$timerResult.Hours -gt 0} {$resultString += ", $($timerResult.Hours) hour$(@{$true = 's'}[$timerResult.Hours -gt 1])"}
{$timerResult.Minutes -gt 0} {$resultString += ", $($timerResult.Minutes) minute$(@{$true = 's'}[$timerResult.Minutes -gt 1])"}
{$timerResult.Seconds -gt 0} {$resultString += ", $($timerResult.Seconds) second$(@{$true = 's'}[$timerResult.Seconds -gt 1])"}
{$timerResult.Milliseconds -gt 0} {$resultString += ", $($timerResult.Milliseconds) millisecond$(@{$true = 's'}[$timerResult.Milliseconds -gt 1])"}
DEFAULT {$resultString = $null}
}
Write-Output ($resultString.TrimStart(', '))
Remove-Variable -Name 'timerStart' -Scope 'Global' -ErrorAction SilentlyContinue
}
else {
Write-Output $null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment