Tuesday, November 11, 2008

Powershell Progress Bar with Time Countdown

I needed to add a 15 minute pause in a script that we were using to create exchange mailboxes and this little Powershell progress bar with a countdown timer worked really nice.

###===========================
### Pause Program for 15 min
### - Matt Brown, 2008
###===========================
$x = 15*60
$length = $x / 100
while($x -gt 0) {
$min = [int](([string]($x/60)).split('.')[0])
$text = " " + $min + " minutes " + ($x % 60) + " seconds left"
Write-Progress "Pausing Script" -status $text -perc ($x/$length)
start-sleep -s 1
$x--
}

2 comments:

  1. I like this. I used it to show a timer while waiting for some files to finish processing. I changed it to recycle a new bar if there were still files pending. Here's my code:

    $numfiles=(Get-ChildItem T:\DVRMS_to_Process).count
    "There are $numfiles files to process"
    $tmr=56*$numfiles
    $length = $tmr / 100
    while((Test-Path T:\DVRMS_to_Process\*) -eq $true) {
    $min = [int](([string]($tmr/60)).split('.')[0])
    $text = " " + $min + " minutes " + ($tmr % 60) + " seconds left"
    Write-Progress "Pausing Script, Waiting for commercials to be cut from $numfiles file(s)" -status $text -perc ($tmr/$length)
    start-sleep -s 1
    $tmr--
    $numfiles=(Get-ChildItem T:\DVRMS_to_Process).count
    If ($tmr -eq 0 -and $numfiles -ge "1"){
    "There are still $numfiles files to process`nStarting new Countdown."
    $tmr=40*$numfiles
    $length = $tmr / 100
    }
    }
    "done pausing"

    ReplyDelete
  2. ###===========================
    ### Pause Program for 15 min
    ### - Matt Brown, 2008
    ###===========================
    $x = 3*60
    $length = $x / 100
    $MinText = "minutes"
    while($x -gt 0) {
    $min = [int](([string]($x/60)).split('.')[0])
    if ($min -eq "1"){$MinText = "minute"}

    if ($min -eq "0") {
    $min = $null
    $MinText = "less than a minute"
    $Beep = [system.console]::Beep(2900,50)
    }

    $text = " " + $min + " $MinText " + ($x % 60) + " seconds left"
    $beep
    Write-Progress "Pausing Script" -status $text -perc ($x/$length)
    start-sleep -s 1
    $x--
    }

    ###=======================
    ### Version 1.1
    ###
    ###=======================

    ReplyDelete