title: PowerShell show elapsed time
body: <p>I have a script that kicks off an event and waits for the user to press any key to stop the script execution. I was trying to find a way to show a timer (how long the script has been running) while waiting for user input at <a href="http://technet.microsoft.com/en-us/library/hh849945.aspx" rel="noreferrer">Read-Host</a>. Is there a way to accomplish this?</p>
  
  <hr/>
  
  <p>This works</p>
  
  <pre><code>$Time = [System.Diagnostics.Stopwatch]::StartNew()
  while ($true) {
      $CurrentTime = $Time.Elapsed
      write-host $([string]::Format("`rTime: {0:d2}:{1:d2}:{2:d2}",
                                    $CurrentTime.hours,
                                    $CurrentTime.minutes,
                                    $CurrentTime.seconds)) -nonewline
      sleep 1
      if ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
          Write-Host "Exiting now"
          break;
      }
  }
  </code></pre>