/* ]]> */
Feb 222011
 

I made an earlier post on this site that demonstrated a simple Count-Down timer that could be used to pause a PowerShell script for x number of minutes and display a countdown timer to indicate how much wait time is remaining. This NEW and IMPROVED CountDown timer expands on that same idea, adding the ability to specify Hours, Minutes, or Seconds and also includes a simple Help feature as well.

The old CountDown timer used the simplistic Function(argument) format as follows:

function CountDown($waitMinutes)

This NEW and IMPROVED version uses the param declaration:

function CountDown() {
    param( [int]$hours=0,
           [int]$minutes=0,
           [int]$seconds=0,
           [switch]$help)

Note that all parameter values are all defaulted to zero and can be specified either by name or positionally specified without names.
For Example, the following invocations are equivalent:

CountDown 0 0 1
is the same as:  Countdown -seconds 1
            or:  Countdown -s 1
Countdown 1
is the same as:  Countdown -hours 1
            or:  Countdown -h 1

There is also a HELP [switch] parameter that, when invoked will display a Here-String that describes the function and its use. This same help output will be triggered if no parameters are passed. Using a here-string makes the help setup very easy as multi-line text can be can be enclosed within a single pair of quotes wrapped with ampersands:

$HelpInfo = @"
Function : CountDown
By       : xb90 at http://poshtips.com
Date     : 02/22/2011
Purpose  : Pauses a script for the specified period of time and displays a count-down timer to indicate the time remaining.
Usage    : Countdown [-Help][-hours n][-minutes n][seconds n]
           where  
              -Help       displays this help
              -Hours n    specify the number of hours (default=0)
              -Minutes n  specify the number of minutes (default=0)
              -Seconds n  specify the number of seconds (default=0)
"@

Ok, that’s enough jabber about the function – let’s take a look:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function CountDown() {
    param(
    [int]$hours=0,
    [int]$minutes=0,
    [int]$seconds=0,
    [switch]$help)
    $HelpInfo = @"
 
Function : CountDown
By       : xb90 at http://poshtips.com
Date     : 02/22/2011
Purpose  : Pauses a script for the specified period of time and displays
 a count-down timer to indicate the time remaining.
Usage    : Countdown [-Help][-hours n][-minutes n][-seconds n]
           where      
           -Help       displays this help
           -Hours n    specify the number of hours (default=0)
           -Minutes n  specify the number of minutes (default=0)
           -Seconds n  specify the number of seconds (default=0)
"@    
 
    if ($help -or (!$hours -and !$minutes -and !$seconds)){
        write-host $HelpInfo
        return
        }
    $startTime = get-date
    $endTime = $startTime.addHours($hours)
    $endTime = $endTime.addMinutes($minutes)
    $endTime = $endTime.addSeconds($seconds)
    $timeSpan = new-timespan $startTime $endTime
    write-host $([string]::format("`nScript paused for {0:#0}:{1:00}:{2:00}",$hours,$minutes,$seconds)) -backgroundcolor black -foregroundcolor yellow
    while ($timeSpan -gt 0) {
        $timeSpan = new-timespan $(get-date) $endTime
        write-host "`r".padright(40," ") -nonewline
        write-host "`r" -nonewline
        write-host $([string]::Format("`rTime Remaining: {0:d2}:{1:d2}:{2:d2}", `
        $timeSpan.hours, `
        $timeSpan.minutes, `
        $timeSpan.seconds)) `
        -nonewline -backgroundcolor black -foregroundcolor yellow
        sleep 1
        }
    write-host ""
    }

Personally, I find this to be so useful that I add it to my $Profile startup script so that it is more easily accessed without the need to add code to a script or call via dot-sourcing. To do this, simply add the code above to your $profile script. In addition I create a shortened alias named “CntDnt” as follows:

new-item -path alias:CntDn -value CountDown |out-null

 Posted by at 11:43 am

  4 Responses to “IMPROVED PowerShell Countdown Timer”

  1. this doesn’t work for me; here’s what I get:

    Script paused for 1:00:00
    Time Remaining: 00:59:59 Time Remaining: 00:59:58 Time Remaining: 00:59:57 Time Remaining: 00:59:56 Time Remaining: 00:59:55 Time Remaining: 00:59:54 Time Remaining: 00:59:53 Time Remaining: 00:59:52

  2. Hey jd,

    I use the ALL THE TIME so it definitely works.

    It looks like the backtick-r part isn’t working for you. That is the escape sequence that issues a carriage-return (without linefeed) to the console so the cursor gets reset back to the first position of the line. The backtick is the single-quote character that has the top leaning to the left.

    Check to make sure you have the back-tick followed by the letter r in your copy of the function. In my code sample this occurs in 3 places: lines 34, 35, and 36.

    I just did a cut-and-paste from this article into PowerShell to confirm and it worked fine.

    Good Luck!

  3. Here is a way to do something similar using PowerShell’s Progress bar:

    while ($i -gt 0) {sleep -s 1;$i–;Write-Progress -Activity “Test Progress Bar Sleep” -SecondsRemaining $i -Status “Sleeping”}

  4. Very useful, thanks for sharing!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Page optimized by WP Minify WordPress Plugin